2020-06-23

原文转载自 「lucifer的网络博客」 ( https://lucifer.ren/blog/2020/06/23/bfs_dfs/ ) By lucifer

预计阅读时间 0 分钟(共 0 个字, 0 张图片, 0 个链接)

本质

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

queue = [0]
visited = set()
steps = 0

while queue:
for _ in range(len(queue)):
cur = queue.pop(0)
if cur in visited: continue
visited.add(cur)
if nums[cur] === target: return (cur, steps)
queue.append(cur - 1)
queue.append(cur + 1)
steps += 1
return (-1, steps)

题目

more_vert