📣 独立日限时特惠: VIP通行证立减$68
查看: 4326| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode]WordLadder有人用python不超时吗

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
如题。之前用C++写的时候就容易超时。用python试了几下没有通过。还是超时。
https://github.com/Linzertorte/L ... aster/WordLadder.py

评分

参与人数 1大米 +3 收起 理由
BreakingDown-_- + 3 很有用的信息!

查看全部评分


上一篇:刷题用java好还是C++好?
下一篇:大家刷Leetcode都是直接在上面打的吗??
🔗
yzl232 2014-7-18 15:48:10 | 只看该作者
全局:
1500 ms. 慢。 过了test


class Solution:
    # @param start, a string
    # @param end, a string
    # @param dict, a set of string
    # @return an integer
    def ladderLength(self, start, end, dict):
        dict.add(end)
        wordLen = len(start)
        candidates = set()
        candidates.add((start, 1))
        while True:
            if len(candidates)==0:  break
            current = set()
            for cur in candidates:
                curWord = cur[0];curLen = cur[1]
                if curWord == end:return curLen
                for i in range(wordLen):
                    part1 = curWord[:i]; part2 = curWord[i+1:] # replace ith char
                    #print part1, part2
                    for j in 'abcdefghijklmnopqrstuvwxyz': # BFS
                        if curWord[i] !=j:
                            nextWord = part1 + j + part2
                            #print nextWord
                            if nextWord == end: return curLen+1
                            if nextWord in dict:
                                current.add((nextWord, curLen + 1))
                                dict.remove(nextWord)
            candidates = current
        return 0
回复

使用道具 举报

🔗
 楼主| Linzertorte 2014-7-18 20:59:26 | 只看该作者
全局:
yzl232 发表于 2014-7-18 15:48
1500 ms. 慢。 过了test

厉害哦。
回复

使用道具 举报

🔗
 楼主| Linzertorte 2014-7-18 21:00:21 | 只看该作者
全局:


word ladder II也用python过掉了
回复

使用道具 举报

🔗
zcy1848 2014-7-19 11:09:03 | 只看该作者
全局:
这题挺卡python的,几个语句顺序不对就超时了
回复

使用道具 举报

🔗
ilnlh 2014-7-20 02:29:22 | 只看该作者
全局:
我的AC版本,交流学习吧


class Solution:
    # @param start, a string
    # @param end, a string
    # @param dict, a set of string
    # @return an integer
    def ladderLength(self, start, end, dict):
        if start == end:
            return 1
   
        all_chars = map(chr, xrange(ord('a'), ord('z') + 1))
        visited = set()
        bfs = [start, None]
        
        # at which distance the following to-be-processed nodes are
        distance = 2
        
        while len(bfs) > 1:
            word = bfs.pop(0)
            if word is None:
                distance += 1
                bfs.append(None)
                continue
            
            for i in xrange(len(word)):
                for char in all_chars:
                    new_word = word[:i] + char + word[i + 1:]
                    if new_word == end:
                        return distance
                    if new_word in dict and new_word not in visited:
                        bfs.append(new_word)
                        visited.add(new_word)
   
        return 0
回复

使用道具 举报

🔗
ilnlh 2014-7-20 02:37:05 | 只看该作者
全局:
        while True:
            if len(candidates)==0:  break
可以改成
        while candidates:

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表