由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - leetcode wordladder2 求助!(solved)
相关主题
这段word ladder II怎么改?大家G电面都是几轮?(附题目)
Leetcode word ladder 求助!问一个word ladder的题目
问两道面试中碰到的题目问一道字符串相关的题目。
双向bfs果然有效率,pyhon 664ms 过wordladder Igoogle电面杯具,贡献题目
转划单词题的优解word ladder II 找所有而不是第一个的最短路径一般咋做的?
M的面试题FB电面的标准就那么高?
求讨论关于Leetcode的WordLadder I的DFS解法这个facebook puzzle样题怎么做?
Course Schedule II DFS版A家面经
相关话题的讨论汇总
话题: newword话题: start话题: word话题: end话题: parents
进入JobHunting版参与讨论
1 (共1页)
t********n
发帖数: 611
1
还是不够快的问题,自己机器上测试结果正确,用了BFS and DFS,不知道还能怎样改
进,请牛牛们指教!
Python code:
class Solution:
# @param start, a string
# @param end, a string
# @param dict, a set of string
# @return a list of lists of string
def findLadders(self, start, end, dict):
def buildPath(end, start, path= []):

path += [end]
if end == start:
path.reverse()
result.append(path)
else:
for p in parents[end]:
newPath = path[:]

buildPath(p, start, newPath)
d = set(dict)
d.add(start)
d.add(end)
parents = {start:None}
current = set([start])
result = []
while current !=[]:
next = set()
for word in current:

for i in xrange(len(word)):
left, right = word[:i], word[i+1:]
for l in "abcdefghijklmnopqrstuvwxyz":
newWord = left + l + right
if newWord != word and newWord != start and newWord
not in current:
if newWord in d:
next.add(newWord)

if newWord not in parents:
parents[newWord] = set([word])

else:
if newWord not in parents[word]:
parents[newWord].add(word)
if end in next:
break
else:
current = next

buildPath( end, start)
return result
t********n
发帖数: 611
2
过了, 参考了某位高人的代码,每次找下级词汇前,把这一级从字典里删除,立刻快
了很多。
Code:
class Solution:
# @param start, a string
# @param end, a string
# @param dict, a set of string
# @return a list of lists of string
def findLadders(self, start, end, dict):
def buildPath(end, start, path= []):
# print
# print "path at beginning:", path
path += [end]
if end == start:
path.reverse()
result.append(path)
else:
for p in parents[end]:
newPath = path[:]
# print "for child ", p, " path is :", newPath
buildPath(p, start, newPath)
d = set(dict)
d.add(start)
d.add(end)
length = len(start)
parents = {}
for word in d:
parents[word] = []
result = []
candidates = [set(), set()]; current=0; previous = 1
candidates[current].add(start)
while True:
previous, current = current, previous
for word in candidates[previous]: d.remove(word)
candidates[current].clear()
for word in candidates[previous]:
# print "word now:", word
for i in xrange(length):
left, right = word[:i], word[i+1:]
for l in "abcdefghijklmnopqrstuvwxyz":
if word[i] != l:
newWord = left + l + right
if newWord in d:
candidates[current].add(newWord)
# print "newWord now:", newWord
# print "in parents?: ", newWord in parents
parents[newWord].append(word)
if len(candidates[current]) == 0: return result
if end in candidates[current]:
break
# print "parents:", parents
buildPath( end, start)
return result

【在 t********n 的大作中提到】
: 还是不够快的问题,自己机器上测试结果正确,用了BFS and DFS,不知道还能怎样改
: 进,请牛牛们指教!
: Python code:
: class Solution:
: # @param start, a string
: # @param end, a string
: # @param dict, a set of string
: # @return a list of lists of string
: def findLadders(self, start, end, dict):
: def buildPath(end, start, path= []):

1 (共1页)
进入JobHunting版参与讨论
相关主题
A家面经转划单词题的优解
Amazon电面纪实M的面试题
请教word ladder解法,大test超时求讨论关于Leetcode的WordLadder I的DFS解法
这个题能有几种解法?Course Schedule II DFS版
这段word ladder II怎么改?大家G电面都是几轮?(附题目)
Leetcode word ladder 求助!问一个word ladder的题目
问两道面试中碰到的题目问一道字符串相关的题目。
双向bfs果然有效率,pyhon 664ms 过wordladder Igoogle电面杯具,贡献题目
相关话题的讨论汇总
话题: newword话题: start话题: word话题: end话题: parents