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

首页

新闻资讯

论坛

温哥华地产

大温餐馆点评

温哥华汽车

温哥华教育

黄页/二手

旅游
搜索:  

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

论坛首页 -> IT人生

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

分页: 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
    潜力帖子 精华帖子 热门帖子
    这里好萧条啊 看来房市买卖很萧条啊
    我认识的朋友,纷纷从美国US加拿大C...
    除了打仗还在发生一件可以改变人类...
    大美丽没有过啊
    接受星期五的建议,买了个空气吸湿器
    挽救IBM:【宇宙丝绸之路】的建设设...
    黄明志【唡个好朋友】
    人可能脱离地球而生存吗?
    美众院通过「大而美」法案
    最新卫片显示:床铺的大坑旁边出现...
    一锅“大杂烩”才叫汉族
    印外长转头跑美国,和3国签下“对抗...
    亚洲72小时倒数开始
    9月3日的中国阅兵有什么好看的?
    奇幻内幕:以色列“团灭”伊朗核科...
    从阿坝州到甘孜州,穿越川西
    “五到八年后再看,疫情后的2023年...
    又看完一部电视剧
    新疆伊犁 赛里木湖 三大草原恰西 喀...
    新疆阿勒泰 五彩滩 喀纳斯 魔鬼城
    在乌鲁木齐看娘娘骑过的汗血宝马
    一张天主教在华发行纸钞略考
    5月2日换币盛况
    维达大师,另类收藏,请您欣赏!
    清代福州台伏钞票
    四川官钱局钞票
    大汉四川军政府军用银票
    今年新币发行计划
    要出一个新的一元
    古董金币
    超级重磅!加拿大要进口中国电动车!
    皮尔今天在温哥华 - 蓝色wave - 保...
    几分钟前,中国强硬反击,征34+50,...
    曼谷高楼直接倒了
    我说我希望特朗普赢,老公气得眼睛...
    知乎?加西网上为什么有老男人喜欢...
    明明有能力统台,大陆为何迟迟不动手?
    貌似ndp稍占上风。。。。。
    今天是感恩节,跟大家道个别,以后...
    咱最后还是投了ndp
    生平第一次被偷车了
    中国会不会武统台湾
    突发:台湾队战胜中国队奥运夺冠,...
    温哥华房姐出事了
    有在看总统辩论的吗?

    最新新闻 热门新闻 热评新闻
    How Game Design Uses Psychology to Boost Online Engagement
    俄罗斯成为第一个正式承认塔利班政权的国家
    正在召回约37万磅完全煮熟的原味火鸡培根
    越美关税协议:不点名地针对中国?
    朝鲜葛麻海滨度假村高调揭幕,但尚无外国游客来访
    28岁中国女子做"小手术"导致子宫穿孔 崩溃
    爱女韩国出道专辑首周仅卖65张 小沈阳强调…
    这个社会留给文科生选的路,不多了
    史上最大规模!120名检察官同时出动调查尹锡悦夫妇
    俄驻地可能有间谍,俄媒披露海军副总司令阵亡细节
    全国性生育补贴政策有望在年内落地,每年补贴...
    罗马仕陷倒闭传闻,充电宝行业能挺住吗?
    杭州气温飙破39°C 未来4天将达40°C 居民哀嚎
    "美国解除对华C919发动机出口禁令" ...
    前NBA扣将惨了 性侵罪成立 9日接受宣判
    陈赫回应"鹿晗患病",原因惹人怀疑
    李兰迪一出手就是王炸,看完预告片,我想说:这尺度,必须5星
    汪小菲彻底放飞自我,犯了富二代的"通病",马筱梅的努力白费了
    接档《以法之名》!央八开播,终于有90后当主角的公安大剧了
    以法之名中的剧抛脸,换个造型就认不出,并非脸盲,是他们太会演
    过气流量"失业阵线联盟":胡一天们的再就业难题
    大温印裔被勒索潮有进展 两人被捕
    朱媛媛遗作浮出水面,一个镜头让人心碎,辛柏青再次引发全网心痛
    戚薇自爆潜规则?500万陪玩5天5夜,不同意就封杀
    《锦绣芳华》首播14广,杨紫出手就是王炸,观众评价出奇一致
    写真集 | 田中美久《更加无忧无虑》
    总局收视榜:《长安的荔枝》第三,《以法之名》第二,第一杀疯了
    E句话|蔡澜助理辟谣:未曾胁迫陈宝莲拍片
    鹿晗面临天价违约金?关晓彤一个举动曝光真相
    《以法之名》里率先出圈的竟然是她
    大温房市销量上月降10% 好消息是
    鹿晗面临天价违约金?关晓彤一个举动曝光真相
    加国赤字要爆炸 联邦无奈要猛裁员
    列治文官员最高薪达58万 超马保定
    刘亦菲缺席输不起?44岁宋佳再拿白玉兰视后,脱掉高跟鞋换人字拖
    乔科维奇温网99胜到手 卫冕冠军鏖战晋级
    特朗普:只要农场主担保,移民工人可以留下
    吃相难看?韩红向贵州捐款930万 刀郎被网友逼捐
    兰博基尼超车爆胎 球星和弟弟惨死
    中共蠢蠢欲动野心转向北方 打台湾先打俄罗斯?
    《酱园弄·悬案》中的"酱园弄",到底在哪里?
    男演员体形多重要?看《书卷一梦》搭李一桐的刘宇宁、王佑硕就知
    同是扫黑题材,《狂飙》封神,《以法之名》争议不断,差别在哪?
    过气流量"失业阵线联盟":胡一天们的再就业难题
    大温印裔被勒索潮有进展 两人被捕

    更多方式阅读论坛:

    Android: 加西网
    [下载]

    Android: 温哥华论坛
    [下载]

    PDA版本: 论坛

    加西网微信

    加西网微博


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

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

    页面生成: 0.0558 秒 and 5 DB Queries in 0.0013 秒