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

首页

新闻资讯

论坛

温哥华地产

大温餐馆点评

温哥华汽车

温哥华教育

黄页/二手

旅游
搜索:  

 论坛通告:  请不要上传第三方有版权的照片,请尊重版权,谢谢   转载新闻请务必注明出处,这些媒体请不要转,谢谢   批评商家需要注意  
 个人空间: XY | 五木森林 | 客观中立而实事求是,唯服理据而杜绝辱骂 | 乱想 | 异乡的世界 | 罗蓬特机器人 | 猪头看世界 | rttp0sui | 顾晓军 | 吕洪来的个人空间 | 一袭绛襦落鹏城,疑似玄女下九天 | 静观云卷云舒 | 细雨飘渺 | Kamxin | 大温房产和地产研究 | Engine0fei | 111 | lxls | 风雨之后 微信号 ID:wenhouwang 问候网 | Notme
 最新求助: 请问谁知道哪里有卖理发的电动推子?   忽然有个疑问:战争时期,加拿大拿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
    潜力帖子 精华帖子 热门帖子
    原住民土地裁决冲击列市私人产权
    头大了!忙晕乎了
    大卫.戈德曼:中国人以实力求和平,...
    ____中学生胡思
    加拿大应该让中国电动车进来
    不行了!忙疯了都要!怎么这么忙!!!
    最喜欢日本的地方就是他们的食物
    我艹,刘婷面熟啊
    麻滴,看得我满身汗毛直竖。。。
    回家
    万能的关税
    瞧把美女给乐的
    川习会在即 传川普将「调降对中关税...
    明娘娘和冰冰桑同志, 请关心敬重下...
    刘厅长,你在哪里啊? 快回来吧? ...
    天津深度游
    mint十月新币
    魁北克 水晶瀑布 加国航拍
    舌尖上的预制菜!
    游了一下多伦多(三)多伦多群岛 湖...
    游了一下多伦多(二)多大 省议会 ...
    拿破仑假金币开模量产,药性还不小!
    今上命书!
    游了一下多伦多(一)市政厅 CN塔 ...
    温哥华的夏天真无奈
    mint八月新币(九月 新币从31楼开始)
    历史的长河之—————安娜海熊猫...
    再次强调一遍!远离加西五毛老头们!
    横批
    俺的版面一页只有三个主贴
    超级重磅!加拿大要进口中国电动车!
    皮尔今天在温哥华 - 蓝色wave - 保...
    几分钟前,中国强硬反击,征34+50,...
    曼谷高楼直接倒了
    我说我希望特朗普赢,老公气得眼睛...
    知乎?加西网上为什么有老流氓刘厅...
    明明有能力统台,大陆为何迟迟不动手?
    貌似ndp稍占上风。。。。。
    今天是感恩节,跟大家道个别,以后...
    咱最后还是投了ndp
    生平第一次被偷车了
    中国会不会武统台湾
    突发:台湾队战胜中国队奥运夺冠,...
    温哥华房姐出事了
    有在看总统辩论的吗?

    最新新闻 热门新闻 热评新闻
    加国留学遇阻 大学周边房租大跳水
    惊传内战大屠杀事件 已有2000人丧命
    9岁男童杀死母亲 竟因这件事…她临死求"抱抱"
    印度首席经济顾问:美元稳定币将挑战全球货币政策
    连续枪击事件!南素里又一商铺受害
    研究揭示:步行锻炼"怎么走"和"走多少"同样重要
    俄罗斯经济观察:莫斯科还能持续支撑前线多久?
    木头姐警告AI存在调整风险! 随着明年利率上升.....
    美联储降息25个基点 将于12月1日起结束量化紧缩
    美军宣布减少在北约东部驻军 欧洲盟国集体焦虑
    美联储祭出组合拳:继续降息25基点+12月结束缩表
    比尔·盖茨立场大转变:气候变化"不会导致人类灭亡"
    "不要特朗普!不要中国!" 抗议者聚集在首尔
    宗馥莉最新公开露面,职位是宏胜集团总裁
    首尔承诺在美投资3500亿美元 特朗普称协定几已达成
    加国20多年前连环性侵 BC的他被控
    中国籍"大头目"在泰国落网 近百人被骗1亿
    超50人在以军空袭中丧生 特朗普:以色列理应反击
    高中生靠ChatGPT策划恐袭 还写大屠杀歌词
    离谱! 澳洲高考突曝"大翻车"8所学校学错知识点
    白宫公布了:"川习会"将于10月30日上午登场
    四川一地采集全县男性血样 官方:完善DNA数据库
    巴西里约沦战场!政府2500兵力突袭毒枭集团 64死
    宛如谍战片!美特工重金诱策反 他听完双腿发抖
    特朗普:预计与习会晤将顺利进行 时长或达4小时
    韩政府有意引进核动力潜艇 韩官员:特朗普表同意
    AI时代奇迹:英伟达市值突破5万亿美元!全球首家
    "除了大豆,美国人竟还想中美谈跨国收养议题"
    中国大学生月均生活费7500元 月均网购7次
    手持导弹玩偶引发争议 中国花滑选手遭国际滑联调查
    东京电影节,范冰冰秒杀全场,生图白又嫩
    婚后17年,蔡少芬:我和张晋的婚姻一地鸡毛
    BC著名国家公园出现令人恐惧狼群
    央行降息 暗示周期结束 加元大涨
    张馨予丈夫何捷:未想过娶女明星?
    这航空开通温哥华至托菲诺新航班
    唐嫣罗晋长期分居,这次是真离了?
    快讯!川普:不确定是否和习谈台湾
    悲壮三文鱼回流 BC省超全观赏攻略
    惨!土地裁决后列市农场主续贷遭绝
    馆长北京直播 男子合影后称"中国台湾一边一国"
    大温这儿汽车起火 女司机不幸丧生
    手持导弹玩偶引发争议 中国花滑选手遭国际滑联调查
    离谱! 澳洲高考突曝"大翻车"8所学校学错知识点
    中国大学生月均生活费7500元 月均网购7次

    更多方式阅读论坛:

    Android: 加西网
    [下载]

    Android: 温哥华论坛
    [下载]

    PDA版本: 论坛

    加西网微信

    加西网微博


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

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

    页面生成: 0.0559 秒 and 5 DB Queries in 0.0015 秒