| 广告联系 | 繁体版 | 手机版 | 微信 | 微博 | 搜索:
欢迎您 游客 | 登录 | 免费注册 | 忘记了密码 | 社交账号注册或登录

首页

新闻资讯

论坛

温哥华地产

大温餐馆点评

温哥华汽车

温哥华教育

黄页/二手

旅游
搜索:  

 论坛通告:  请不要上传第三方有版权的照片,请尊重版权,谢谢   转载新闻请务必注明出处,这些媒体请不要转,谢谢   批评商家需要注意  
 个人空间: 罗蓬特机器人 | Notme | 乱想 | XY | 五木森林 | 猪头看世界 | Amy Yi | 异乡的世界 | rxmei | 53757645468 | 湖里湖涂 | reddragon | 顾晓军 | 客观中立而实事求是,唯服理据而杜绝辱骂 | 静观云卷云舒 | 逸言堂 | 细雨飘渺 | 北极熊要去北极老家 | Invisible world | 一袭绛襦落鹏城,疑似玄女下九天
 最新求助: 请问谁知道哪里有卖理发的电动推子?   忽然有个疑问:战争时期,加拿大拿PR卡未入籍的永久居民会被强制服兵役吗?   这个银条   如何修改会员名?
 论坛转跳:
     发帖回帖获取加西镑, 兑换精彩礼物

论坛首页 -> IT人生

Leetcode Hacking Practice -- Solution in Python Part 1 (发表于11年前)

分页: 1, 2, 3 ... 30, 31, 32  下一页  



回复主题  图片幻灯展示  增添帖子到书签中  给帖子中的发贴者批量赠送献花或者花篮    |##| -> |=|        发表新主题
阅读上一个主题 :: 阅读下一个主题  
作者 正文
webdriver
(只看此人)




文章 时间: 2014-9-29 23:12 引用回复
LeetCode (used to call i has 1337 code) is a social platform for preparing IT technical interviews. We strive to provide you with the best learning experience in preparing interviews for companies in the IT industry.

To be successful in a technical interview, we believe it is mainly repeating these three important steps:

Code. Read. Discuss.

We strive to provide you the LeetCode platform as the ultimate solution for preparing technical interviews.

1. Code -> Code solution using the Online Judge system.
2. Read -> Read high quality article featuring in-depth thought process. Also read other LeetCoders’ code.
3. Discuss -> Discuss your thoughts about the problem with other LeetCoders.

We hope that through our platform, you will grow into a LeetCoder. Not only will you be successful in all of your interviews, and most importantly, you will be a better coder in general !

--------------

All solutions are in Python!
 
花篮
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
楼主 | 电梯直达
阅读会员资料 发送站内短信 主题 User photo gallery 礼物  
webdriver
(只看此人)




文章 时间: 2014-9-29 23:19 引用回复
S算法问题: Add Binary
问题描述:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
 
花篮
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
沙发 | 返回顶端
阅读会员资料 发送站内短信 主题 User photo gallery 礼物  
webdriver
(只看此人)



文章 时间: 2014-9-29 23:20 引用回复
算法问题推荐解法来了...

'1'-'0' = 1.

解法(Python)
代码:
class Solution:
    def addBinary(self, a, b):
        return bin(int(a, 2) + int(b, 2)).split('b')[1]

    def addBinary(self, a, b):
       """Sometimes built-in function cheats too much.
       """
        res, carry, len_a, len_b, i = "", 0, len(a), len(b), 0
        for i in range(max(len_a, len_b)):
            sum = carry
            if i < len_a:
                sum += int(a[-(i + 1)])
            if i < len_b:
                sum += int(b[-(i + 1)])
            carry = sum / 2
            sum = sum % 2
            res = "{0}{1}".format(sum, res)
        if carry == 1:
            res = "1" + res
        return res

    # def addBinary(self, a, b):
    #    """Using carry without sum is also fine. But the readability sucks.
    #    """
    #     res, carry, len_a, len_b, i = "", 0, len(a), len(b), 0
    #     for i in range(max(len_a, len_b)):
    #         if i < len_a:
    #             carry += int(a[-(i + 1)])
    #         if i < len_b:
    #             carry += int(b[-(i + 1)])
    #         res = "{0}{1}".format(carry % 2, res)
    #         carry = carry / 2
    #     if carry == 1:
    #         res = "1" + res
    #     return res
 
花篮
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
板凳 | 返回顶端
阅读会员资料 发送站内短信 主题 User photo gallery 礼物  
webdriver
(只看此人)



文章 时间: 2014-9-29 23:22 引用回复
S算法问题: Add Two Numbers
问题描述:
You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
 
花篮
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
地板 | 返回顶端
阅读会员资料 发送站内短信 主题 User photo gallery 礼物  
webdriver
(只看此人)



文章 时间: 2014-9-29 23:23 引用回复
算法问题推荐解法来了...

dummy head...


解法(Python)
代码:
class Solution:
    def addTwoNumbers(self, l1, l2):
        dummy = ListNode(0)
        current, carry = dummy, 0
        while l1 != None or l2 != None:
            res = carry
            if l1 != None:
                res += l1.val
                l1 = l1.next
            if l2 != None:
                res += l2.val
                l2 = l2.next
            carry, res = res / 10, res % 10
            current.next = ListNode(res)
            current = current.next
        if carry == 1:
            current.next = ListNode(1)
        return dummy.next
 
花篮
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
5 楼 | 返回顶端
阅读会员资料 发送站内短信 主题 User photo gallery 礼物  
webdriver
(只看此人)



文章 时间: 2014-9-29 23:25 引用回复
S算法问题: Anagrams
问题描述:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
 
花篮
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
6 楼 | 返回顶端
阅读会员资料 发送站内短信 主题 User photo gallery 礼物  
webdriver
(只看此人)



文章 时间: 2014-9-29 23:26 引用回复
算法问题推荐解法来了...

Sort the string to see if they're anagrams.
Solution 1 is simpler than 2.

解法(Python)
代码:
class Solution:
    def anagrams(self, strs):
        anagram_map, res = {}, []
        for str in strs:
            sorted_str = ("").join(sorted(str))
            if sorted_str in anagram_map:
                anagram_map[sorted_str].append(str)
            else:
                anagram_map[sorted_str] = [str]
        for anagrams in anagram_map.values():
            if len(anagrams) > 1:
                res += anagrams
        return res

    # def anagrams(self, strs):
    #     """List comprehension may be more elegant but less readable here.
    #     """
    #     anagram_map = {}
    #     for str in strs:
    #         sorted_str = ("").join(sorted(str))
    #         if sorted_str in anagram_map:
    #             anagram_map[sorted_str].append(str)
    #         else:
    #             anagram_map[sorted_str] = [str]
    #     return [anagram for anagrams in anagram_map.values() if len(anagrams) > 1 for anagram in anagrams]
 
花篮
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
7 楼 | 返回顶端
阅读会员资料 发送站内短信 主题 User photo gallery 礼物  
webdriver
(只看此人)



文章 时间: 2014-9-29 23:29 引用回复
S算法问题: Balanced Binary Tree
问题描述:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which
the depth of the two subtrees of every node never differ by more than 1.
 
花篮
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
8 楼 | 返回顶端
阅读会员资料 发送站内短信 主题 User photo gallery 礼物  
webdriver
(只看此人)



文章 时间: 2014-9-29 23:30 引用回复
算法问题推荐解法来了...

DFS.

解法(Python)
代码:
class Solution:
    def isBalanced(self, root):
        return self.getHeight(root) != -1
   
    def getHeight(self, root):
        if root == None:
            return 0
        left_height, right_height = self.getHeight(root.left), self.getHeight(root.right)
        if left_height < 0 or right_height < 0 or math.fabs(left_height - right_height) > 1:
            return -1
        return max(left_height, right_height) + 1
 
花篮
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
9 楼 | 返回顶端
阅读会员资料 发送站内短信 主题 User photo gallery 礼物  
webdriver
(只看此人)



文章 时间: 2014-9-29 23:33 引用回复
S算法问题: Best Time to Buy and Sell Stock II
问题描述:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like
(ie, buy one and sell one share of the stock multiple times).
However, you may not engage in multiple transactions at the same time
(ie, you must sell the stock before you buy again).
 
花篮
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
10 楼 | 返回顶端
阅读会员资料 发送站内短信 主题 User photo gallery 礼物  
 
回复主题     |##| -> |=|     论坛首页 -> IT人生 所有的时间均为 美国太平洋时间
1页,共32 分页: 1, 2, 3 ... 30, 31, 32  下一页  


注:
  • 以上论坛所有发言仅代表发帖者个人观点, 并不代表本站观点或立场, 加西网对此不负任何责任。
  • 投资理财及买房卖房版面的帖子不构成投资建议。投资有风险,责任请自负
  • 对二手买卖中的虚假信息,买卖中的纠纷等均与本站无关。
  • 黄页热门商家 免费个人广告
    发布商业广告

    不能在本论坛发表新主题
    不能在本论坛回复主题
    不能在本论坛编辑自己的文章
    不能在本论坛删除自己的文章
    不能在本论坛发表投票
    不能在这个论坛添加附件
    可以在这个论坛下载文件

    论坛转跳: 

    webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver
    潜力帖子 精华帖子 热门帖子
    市场质疑 BC投机空置税2026年上调
    加西最“有才”的女光棍子的名言
    戳破谣言 敬悼英魂:毛岸英烈士牺牲...
    香港火&#128293;
    IBM,奶茶妹项目,怎么办?
    恭喜华川粉,好日子要来了
    “西方太拖沓,欧洲顶尖核物理学家...
    满江红 毛岸英 zt
    重磅!徐勤先六四辩护资料流出
    美国感恩节
    毛岸英烈士牺牲七十周年,把颠倒的...
    迄今发现七十多位往生者
    汉奸帖爆火日本网络,对中国游客发...
    痛击高市 倭寇欠揍 zt
    ____ 不知道这个预测准不准
    迈阿密(一)
    加拿大全国各地兑换纪念【无名烈士...
    2025纪念无名烈士加拿大2元流通硬币
    自藏求精!
    西岸快线30周年纪念品
    天津深度游(二)
    天津深度游
    mint十月新币 (十一月新币从25楼开始)
    魁北克 水晶瀑布 加国航拍
    舌尖上的预制菜!
    游了一下多伦多(三)多伦多群岛 湖...
    游了一下多伦多(二)多大 省议会 ...
    拿破仑假金币开模量产,药性还不小!
    今上命书!
    游了一下多伦多(一)市政厅 CN塔 ...
    超级重磅!加拿大要进口中国电动车!
    皮尔今天在温哥华 - 蓝色wave - 保...
    几分钟前,中国强硬反击,征34+50,...
    曼谷高楼直接倒了
    我说我希望特朗普赢,老公气得眼睛...
    知乎?加西网上为什么有老流氓刘厅...
    明明有能力统台,大陆为何迟迟不动手?
    貌似ndp稍占上风。。。。。
    今天是感恩节,跟大家道个别,以后...
    咱最后还是投了ndp
    生平第一次被偷车了
    中国会不会武统台湾
    突发:台湾队战胜中国队奥运夺冠,...
    温哥华房姐出事了
    有在看总统辩论的吗?

    最新新闻 热门新闻 热评新闻
    史上最大理赔?香港灾民每户能有多少赔偿
    全年最低价就在今天!值得买的黑五好东西都在这!
    大爆!《亦舞之城》今日空降,四大平台纷纷甩出"王炸剧"对决
    香港大火 美表示哀悼 拘捕更多相关人员
    AI芯片步入战国时代! 英伟达垄断被打破
    红色大院的女儿们, 饥荒年也尝"小球藻"
    苹果率先捐款 港府明起降半旗3天
    周末大温超市优惠抢先看 扫货指南
    警示!研究:年轻人患癌复发风险高
    地震 安省着手接管房地产监管机构
    高市早苗民调支持率高 面临对华关系、通胀挑战
    假香水:法国海关突击行动查获82,000瓶
    BC近半餐厅经营困难 许多有倒闭风险
    反传!带3娃徒步生还 父亲被控虐童
    枫树岭为拓宽这要道收购53处房产
    香港宏福苑大火:港府治理承受重大挑战
    在这里 美国会不会搬起石头砸自己的脚
    世界首台660兆瓦超超临界循环流化床锅炉通过鉴定
    卢卡申科神吐槽:我哪配当调解人啊,我不....
    100个"野人",靠"荒野求生" 救活了一座山
    四川民企造出高超音速导弹?企业回应:已量产
    火灾救援:无人机如何才能真正解决高空灭火难题?
    佩斯科夫:俄方已收到美乌达成一致的计划细节
    深圳少年捅杀14岁女同学 一审判囚终身
    美媒猜测:马杜罗若决定流亡海外,最可能去这国家
    76岁泰国前总理他信入狱后全家福首度公开
    香港大火是人祸? 港府巡查16次仅提醒防火
    精神分裂者杀害祖孙三口,"这两年没见他吃过药"
    特斯拉前高管:拆解中国电动汽车后发现"妙招"
    单季净利翻倍难敌18亿判罚冲击,金龙鱼蒸发百亿
    华人告诉高市早苗 日本有一个中国没有的优势
    美国要施压加国减移民?移民部长说
    香港网友愤怒:第一次这么希望中共和台湾开战
    卡尼刚签管道协议 这部长愤而辞职
    加国黑色星期五手机直销最佳优惠
    香港大火是人祸?中企生产材料不合格
    两问香港大埔火灾:施工为何采用竹棚架?为何难救?
    乌二号人物住所被搜查,英媒:越来越接近泽连斯基
    美媒猜测:马杜罗若决定流亡海外,最可能去这国家
    香港大火,一个致命短板,内地很常见
    赖清德清洗台军"异己",台海军司令唐华被贬
    香港数千巿民家毁人亡 李家超该当何罪!
    76岁泰国前总理他信入狱后全家福首度公开
    卢卡申科神吐槽:我哪配当调解人啊,我不....
    世界首台660兆瓦超超临界循环流化床锅炉通过鉴定

    更多方式阅读论坛:

    Android: 加西网
    [下载]

    Android: 温哥华论坛
    [下载]

    PDA版本: 论坛

    加西网微信

    加西网微博


    Powered by phpBB 2.0.8
    Terms & Conditions    Privacy Policy    Political ADs    Activities Agreement    Contact Us    Sitemap    

    加西网为北美中文网传媒集团旗下网站

    页面生成: 0.0593 秒 and 7 DB Queries in 0.0021 秒