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

首页

新闻资讯

论坛

温哥华地产

大温餐馆点评

温哥华汽车

温哥华教育

黄页/二手

旅游
搜索:  

 论坛通告:  加西磅停用通知   请不要上传第三方有版权的照片,请尊重版权,谢谢   转载新闻请务必注明出处,这些媒体请不要转,谢谢   批评商家需要注意  
 个人空间: 罗蓬特机器人 | 细雨飘渺 | XY | 五木森林 | 真情Z下海 | 乱想 | 花随风 | 猪头看世界 | 湖里湖涂 | 静观云卷云舒 | 顾晓军 | Amy Yi | Invisible world | rttp0sui | 大温房产和地产研究 | zhijpjun0 | tjh96 | 客观中立而实事求是,唯服理据而杜绝辱骂 | HFWATER | 吕洪来的个人空间
 最新求助: 请问谁知道哪里有卖理发的电动推子?   忽然有个疑问:战争时期,加拿大拿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
    潜力帖子 精华帖子 热门帖子
    曹,川普中期选举稳了,又躲过了子...
    关于: 加拿大皇家铸币厂的网站
    唐师曾得了白血病
    惨了, 床铺要翻案了
    伊朗油井报废
    本宫在苏丹阿都沙末大厦里泡了一下午
    中国不再允许国际领养了
    伊朗战争打得越久,对俄国越有利
    毒舌,学习一下米国警察如何持手枪
    现场直播:毒舌的大统领瞌睡了
    现在能看出来以前哪些人是混镑的吗?
    特朗普最大的,毫无悬念的成就。。。
    ____静哥哥,偶原来也以为大嘴的tac...
    特朗普大学。。。
    继续挽救IBM。
    Royal Canadian Mint 皇家铸币厂202...
    Royal Canadian Mint 皇家铸币厂202...
    TransLink推出限量版Hello Kitty公交卡
    换币活动
    加拿大将发行纪念皇家军团(The Roy...
    加拿大将发行第二枚夜光币
    Royal Canadian Mint 皇家铸币厂202...
    奥兰多(二)迪士尼动物王国 未来世界...
    奥兰多(一)城市地标及海洋世界
    Royal Canadian Mint 2026年2月新币
    坎昆(一)
    推荐一个digital的手持放大镜
    RCM 2026年1月新币
    拆卷有惊喜
    炉关寻觅记
    特朗普又干了件冒天下之大不韪的事...
    本宫钢琴弹奏原声第1弹 一首前奏曲
    谢谢管理员秉公执法废除reddragon的id
    超级重磅!加拿大要进口中国电动车!
    皮尔今天在温哥华 - 蓝色wave - 保...
    几分钟前,中国强硬反击,征34+50,...
    曼谷高楼直接倒了
    我说我希望特朗普赢,老公气得眼睛...
    知乎?加西网上为什么有老流氓刘厅...
    明明有能力统台,大陆为何迟迟不动手?
    貌似ndp稍占上风。。。。。
    今天是感恩节,跟大家道个别,以后...
    咱最后还是投了ndp
    生平第一次被偷车了
    中国会不会武统台湾

    最新新闻 热门新闻 热评新闻
    痛风病人吃辣,不用多久或有4个变化
    假球?湖人这波操作,比输球还让人心寒
    浪姐三公小考剧透:曾沛慈王蒙前二,安崎垫底孙怡再陷淘汰危机
    西洋参春天喝太养人 5款黄金搭配安睡整晚
    陈晓旭去世19年,丈夫已还俗,再婚生女
    20亿美元泡汤,Manus不值得同情(图
    颠覆iPhone 传OpenAI抛出震撼弹
    太阳突然醒了 7小时两次释放X级耀斑
    美剧片场全是戏?这位演员说反了
    担忧暗杀!老黄带5个保镖,奥特曼们沦为靶子
    有人只睡4小时也很精神 而你睡8小时还困?
    不靠美国也能称霸,比亚迪大秀肌肉
    下月起,房子、存款将迎来"大变局"
    光环碎了一地?当53万海归遇上1222万毕业生
    多位顶级专家离职....华为迎来前所未有危机
    杨幂脸被蒸汽眼罩烫伤上热搜 很多人都在用
    AI造假证 伪造死亡证明逃债 揭秘反催收黑产链条
    菲美及盟友在南中国海举行演习 日本下阶段登场
    中国女子斯里兰卡遇害案2人落网 主谋身上搜出毒品
    摆脱对中国依赖 美国分散稀土源锁定马来西亚巴西
    成枪手目标 川普火大:我不是强暴犯、恋童癖
    DeepSeek发表新AI模型 路透:市场反应冷淡
    辐射不是最大杀手!切尔诺贝利竟成动物意外天堂
    大温女子遭假客服诈骗 损失数千元
    预测BC房屋销售2026低迷 明年反弹
    ICE执法惹反感 她建议变「NICE」 川普回:改下去
    欧盟推进"欧洲制造"战略 中国批评新法案构成歧视
    斯德哥尔摩国际和平研究所 全球军费开支持续增长
    列治文中心尊贵社区万尺大地豪宅
    大温油价涨 回到燃料税暂停前水平
    大温油价涨 回到燃料税暂停前水平
    预测BC房屋销售2026低迷 明年反弹
    杨幂脸被蒸汽眼罩烫伤上热搜 很多人都在用
    DeepSeek发表新AI模型 路透:市场反应冷淡
    成枪手目标 川普火大:我不是强暴犯、恋童癖
    摆脱对中国依赖 美国分散稀土源锁定马来西亚巴西
    中国女子斯里兰卡遇害案2人落网 主谋身上搜出毒品
    菲美及盟友在南中国海举行演习 日本下阶段登场
    AI造假证 伪造死亡证明逃债 揭秘反催收黑产链条
    辐射不是最大杀手!切尔诺贝利竟成动物意外天堂
    大温女子遭假客服诈骗 损失数千元
    ICE执法惹反感 她建议变「NICE」 川普回:改下去
    欧盟推进"欧洲制造"战略 中国批评新法案构成歧视
    斯德哥尔摩国际和平研究所 全球军费开支持续增长
    列治文中心尊贵社区万尺大地豪宅

    更多方式阅读论坛:

    Android: 加西网
    [下载]

    Android: 温哥华论坛
    [下载]

    PDA版本: 论坛

    加西网微信

    加西网微博


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

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

    页面生成: 0.0374 秒 and 7 DB Queries in 0.0023 秒