单词接龙 python_leetcode 127. 单词接龙 126. 单词接龙 II

预处理+广度优先遍历

这个代码不超时了,但是十分缓慢,官方题解说可以使用双向广搜提高速度,我这里没有写双向的代码

127 题

class Solution:

def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:

if beginWord not in wordList:

wordList.append(beginWord)

if endWord not in wordList:

return 0

s = wordList.index(beginWord)

e = wordList.index(endWord)

f = [False] * len(wordList)

import queue

q = queue.Queue()

f[s] = True

q.put((s, 0, []))

edges = {}

for i, word in enumerate(wordList):

w = list(word)

for j in range(len(w)):

t = w[j]

w[j] = '*'

ww = ''.join(w)

if ww not in edges:

edges[ww] = []

edges[ww].append(i)

w[j] = t

mins = -1

# ans = []

while not q.empty():

cur, step, path = q.get()

path.append(wordList[cur])

f[cur] = True

if cur == e:

if mins == -1:

mins = step

return step+1

# else:

# if mins < step:

# return ans

# ans.append(path)

w = list(wordList[cur])

for j in range(len(w)):

t = w[j]

w[j] = '*'

ww = ''.join(w)

for x in edges[ww]:

if f[x] == False:

q.put((x, step+1, path[::]))

w[j] = t

return 0

126 题

class Solution:

def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:

if beginWord not in wordList:

wordList.append(beginWord)

if endWord not in wordList:

return []

s = wordList.index(beginWord)

e = wordList.index(endWord)

f = [False] * len(wordList)

import queue

q = queue.Queue()

f[s] = True

q.put((s, 0, []))

edges = {}

for i, word in enumerate(wordList):

w = list(word)

for j in range(len(w)):

t = w[j]

w[j] = '*'

ww = ''.join(w)

if ww not in edges:

edges[ww] = []

edges[ww].append(i)

w[j] = t

mins = -1

ans = []

while not q.empty():

cur, step, path = q.get()

path.append(wordList[cur])

f[cur] = True

if cur == e:

if mins == -1:

mins = step

else:

if mins < step:

return ans

ans.append(path)

w = list(wordList[cur])

for j in range(len(w)):

t = w[j]

w[j] = '*'

ww = ''.join(w)

for x in edges[ww]:

if f[x] == False:

q.put((x, step+1, path[::]))

w[j] = t

return ans

超时代码

127 题

class Solution:

def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:

if beginWord not in wordList:

wordList.append(beginWord)

if endWord not in wordList:

return 0

# wordList.append(endWord)

s = wordList.index(beginWord)

e = wordList.index(endWord)

f = [False] * len(wordList)

import queue

q = queue.Queue()

f[s] = True

q.put((s, 0, []))

def can_go(w1, w2):

c = 0

assert len(w1) == len(w2)

for i in range(len(w1)):

if w1[i] != w2[i]:

c += 1

if c > 1:

return False

return c == 1

mins = -1

while not q.empty():

cur, step, path = q.get()

path.append(wordList[cur])

f[cur] = True

if cur == e:

if mins == -1:

mins = step

return step+1

for i in range(len(wordList)):

if i != cur and not f[i]:

if can_go(wordList[cur], wordList[i]):

q.put((i, step+1, path[::]))

return 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值