Skip to content

Commit a6c8e32

Browse files
author
lifeiyang
committed
2022-11-22-17:07:25
1 parent 4634d0e commit a6c8e32

9 files changed

+365
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
更新于 : 2022-11-20-21:33:57
1+
更新于 : 2022-11-22-17:07:25
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from typing import Optional, List
2+
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
11+
# leetcode submit region begin(Prohibit modification and deletion)
12+
# Definition for a binary tree node.
13+
# class TreeNode:
14+
# def __init__(self, val=0, left=None, right=None):
15+
# self.val = val
16+
# self.left = left
17+
# self.right = right
18+
class Solution:
19+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
20+
# 边缘情况
21+
if root is None:
22+
return []
23+
24+
self.res = []
25+
self.traverse(root, targetSum, [root.val])
26+
return self.res
27+
28+
def traverse(self, root, target_sum, track_list):
29+
30+
# 处理当前节点
31+
if root.left is None and root.right is None:
32+
# 叶子节点了
33+
if sum(track_list) == target_sum:
34+
self.res.append(track_list.copy())
35+
else:
36+
return
37+
38+
if root.left is not None:
39+
self.traverse(root.left, target_sum, track_list + [root.left.val])
40+
41+
if root.right is not None:
42+
self.traverse(root.right, target_sum, track_list + [root.right.val])
43+
44+
45+
# leetcode submit region end(Prohibit modification and deletion)
46+
47+
48+
if __name__ == "__main__":
49+
solution = Solution()
50+
root = [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1]
51+
52+
tree_root = TreeNode(root.pop(0))
53+
q = [tree_root]
54+
55+
while len(root) > 0:
56+
temp_root = q.pop(0)
57+
value = root.pop(0)
58+
if value is not None:
59+
left = TreeNode(value)
60+
temp_root.left = left
61+
q.append(left)
62+
63+
value = root.pop(0)
64+
if value is not None:
65+
right = TreeNode(value)
66+
temp_root.right = right
67+
q.append(right)
68+
69+
print(solution.pathSum(tree_root, 22))

leetcode/editor/cn/[207]课程表.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import List
2+
3+
4+
# leetcode submit region begin(Prohibit modification and deletion)
5+
class Solution:
6+
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
7+
# 实现 DFS
8+
graph = self.build_graph(prerequisites)
9+
10+
self.visited = [False] * numCourses
11+
self.on_path = [False] * numCourses
12+
self.has_cycle = False
13+
14+
for node in range(numCourses):
15+
self.traverse(graph, node)
16+
17+
return not self.has_cycle
18+
19+
def traverse(self, graph, node):
20+
if self.on_path[node]:
21+
self.has_cycle = True
22+
23+
if self.visited[node]:
24+
return
25+
26+
self.visited[node] = True
27+
self.on_path[node] = True
28+
for neighbor in graph[node]:
29+
self.traverse(graph, neighbor)
30+
self.on_path[node] = False
31+
32+
def build_graph(self, prerequisites):
33+
from collections import defaultdict
34+
graph = defaultdict(list)
35+
for to_node, from_node in prerequisites:
36+
graph[from_node].append(to_node)
37+
return graph
38+
39+
40+
# leetcode submit region end(Prohibit modification and deletion)
41+
42+
43+
if __name__ == "__main__":
44+
solution = Solution()
45+
print(solution.canFinish(20, [[0, 10], [3, 18], [5, 5], [6, 11], [11, 14], [13, 1], [15, 1], [17, 4]]), False)
46+
print(solution.canFinish(2, [[1, 0], [0, 1]]), False)
47+
print(solution.canFinish(2, [[1, 0]]), True)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from typing import List
2+
3+
4+
# leetcode submit region begin(Prohibit modification and deletion)
5+
class Solution:
6+
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
7+
graph = self.build_graph(prerequisites)
8+
9+
self.visited = [False] * numCourses
10+
self.on_path = [False] * numCourses
11+
self.has_cycle = False
12+
13+
self.post_order = []
14+
15+
for node in range(numCourses):
16+
self.traverse(graph, node)
17+
if self.has_cycle:
18+
return []
19+
else:
20+
return list(reversed(self.post_order))
21+
22+
def traverse(self, graph, node):
23+
if self.on_path[node]:
24+
self.has_cycle = True
25+
if self.visited[node]:
26+
return
27+
28+
self.visited[node] = True
29+
self.on_path[node] = True
30+
31+
for neighbor in graph[node]:
32+
self.traverse(graph, neighbor)
33+
34+
self.post_order.append(node)
35+
36+
self.on_path[node] = False
37+
38+
def build_graph(self, prerequisites):
39+
from collections import defaultdict
40+
graph = defaultdict(list)
41+
42+
for to_node, from_node in prerequisites:
43+
graph[from_node].append(to_node)
44+
45+
return graph
46+
47+
48+
# leetcode submit region end(Prohibit modification and deletion)
49+
50+
if __name__ == "__main__":
51+
solution = Solution()
52+
print(solution.findOrder(2, [[1, 0]]), [0, 1])
53+
print(solution.findOrder(4, [[1, 0], [2, 0], [3, 1], [3, 2]]), [0, 2, 1, 3])
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
# leetcode submit region begin(Prohibit modification and deletion)
5+
class Solution:
6+
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
7+
self.res = []
8+
self.traverse(graph, 0, len(graph) - 1, [])
9+
return self.res
10+
11+
def traverse(self, graph, node_idx, target_idx, track_list):
12+
if node_idx == target_idx:
13+
self.res.append(track_list + [target_idx])
14+
15+
track_list.append(node_idx)
16+
for neighbor_node in graph[node_idx]:
17+
self.traverse(graph, neighbor_node, target_idx, track_list)
18+
track_list.pop(-1)
19+
20+
21+
# leetcode submit region end(Prohibit modification and deletion)
22+
23+
24+
if __name__ == "__main__":
25+
solution = Solution()
26+
print(solution.allPathsSourceTarget([[4, 3, 1], [3, 2, 4], [3], [4], []]))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>给你二叉树的根节点 <code>root</code> 和一个整数目标和 <code>targetSum</code> ,找出所有 <strong>从根节点到叶子节点</strong> 路径总和等于给定目标和的路径。</p>
2+
3+
<p><strong>叶子节点</strong> 是指没有子节点的节点。</p>
4+
5+
<div class="original__bRMd">
6+
<div>
7+
<p>&nbsp;</p>
8+
</div>
9+
</div>
10+
11+
<p><strong>示例 1:</strong></p>
12+
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
13+
<pre>
14+
<strong>输入:</strong>root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
15+
<strong>输出:</strong>[[5,4,11,2],[5,8,4,5]]
16+
</pre>
17+
18+
<p><strong>示例 2:</strong></p>
19+
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
20+
<pre>
21+
<strong>输入:</strong>root = [1,2,3], targetSum = 5
22+
<strong>输出:</strong>[]
23+
</pre>
24+
25+
<p><strong>示例 3:</strong></p>
26+
27+
<pre>
28+
<strong>输入:</strong>root = [1,2], targetSum = 0
29+
<strong>输出:</strong>[]
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
34+
<p><strong>提示:</strong></p>
35+
36+
<ul>
37+
<li>树中节点总数在范围 <code>[0, 5000]</code> 内</li>
38+
<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
39+
<li><code>-1000 &lt;= targetSum &lt;= 1000</code></li>
40+
</ul>
41+
42+
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>回溯</li><li>二叉树</li></div></div><br><div><li>👍 867</li><li>👎 0</li></div>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>你这个学期必须选修 <code>numCourses</code> 门课程,记为&nbsp;<code>0</code>&nbsp;&nbsp;<code>numCourses - 1</code> 。</p>
2+
3+
<p>在选修某些课程之前需要一些先修课程。 先修课程按数组&nbsp;<code>prerequisites</code> 给出,其中&nbsp;<code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> ,表示如果要学习课程&nbsp;<code>a<sub>i</sub></code> 则 <strong>必须</strong> 先学习课程&nbsp; <code>b<sub>i</sub></code><sub> </sub>。</p>
4+
5+
<ul>
6+
<li>例如,先修课程对&nbsp;<code>[0, 1]</code> 表示:想要学习课程 <code>0</code> ,你需要先完成课程 <code>1</code> 。</li>
7+
</ul>
8+
9+
<p>请你判断是否可能完成所有课程的学习?如果可以,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
10+
11+
<p>&nbsp;</p>
12+
13+
<p><strong>示例 1:</strong></p>
14+
15+
<pre>
16+
<strong>输入:</strong>numCourses = 2, prerequisites = [[1,0]]
17+
<strong>输出:</strong>true
18+
<strong>解释:</strong>总共有 2 门课程。学习课程 1 之前,你需要完成课程 0 。这是可能的。</pre>
19+
20+
<p><strong>示例 2:</strong></p>
21+
22+
<pre>
23+
<strong>输入:</strong>numCourses = 2, prerequisites = [[1,0],[0,1]]
24+
<strong>输出:</strong>false
25+
<strong>解释:</strong>总共有 2 门课程。学习课程 1 之前,你需要先完成​课程 0 ;并且学习课程 0 之前,你还应先完成课程 1 。这是不可能的。</pre>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong>提示:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= numCourses &lt;= 10<sup>5</sup></code></li>
33+
<li><code>0 &lt;= prerequisites.length &lt;= 5000</code></li>
34+
<li><code>prerequisites[i].length == 2</code></li>
35+
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; numCourses</code></li>
36+
<li><code>prerequisites[i]</code> 中的所有课程对 <strong>互不相同</strong></li>
37+
</ul>
38+
39+
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>图</li><li>拓扑排序</li></div></div><br><div><li>👍 1460</li><li>👎 0</li></div>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<p>现在你总共有 <code>numCourses</code> 门课需要选,记为&nbsp;<code>0</code>&nbsp;&nbsp;<code>numCourses - 1</code>。给你一个数组&nbsp;<code>prerequisites</code> ,其中 <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> ,表示在选修课程 <code>a<sub>i</sub></code> 前 <strong>必须</strong> 先选修&nbsp;<code>b<sub>i</sub></code> 。</p>
2+
3+
<ul>
4+
<li>例如,想要学习课程 <code>0</code> ,你需要先完成课程&nbsp;<code>1</code> ,我们用一个匹配来表示:<code>[0,1]</code> 。</li>
5+
</ul>
6+
7+
<p>返回你为了学完所有课程所安排的学习顺序。可能会有多个正确的顺序,你只要返回 <strong>任意一种</strong> 就可以了。如果不可能完成所有课程,返回 <strong>一个空数组</strong> 。</p>
8+
9+
<p>&nbsp;</p>
10+
11+
<p><strong>示例 1:</strong></p>
12+
13+
<pre>
14+
<strong>输入:</strong>numCourses = 2, prerequisites = [[1,0]]
15+
<strong>输出:</strong>[0,1]
16+
<strong>解释:</strong>总共有 2 门课程。要学习课程 1,你需要先完成课程 0。因此,正确的课程顺序为 <span><code>[0,1] 。</code></span>
17+
</pre>
18+
19+
<p><strong>示例 2:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
23+
<strong>输出:</strong>[0,2,1,3]
24+
<strong>解释:</strong>总共有 4 门课程。要学习课程 3,你应该先完成课程 1 和课程 2。并且课程 1 和课程 2 都应该排在课程 0 之后。
25+
因此,一个正确的课程顺序是&nbsp;<span><code>[0,1,2,3]</code></span> 。另一个正确的排序是&nbsp;<span><code>[0,2,1,3]</code></span> 。</pre>
26+
27+
<p><strong>示例 3:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>numCourses = 1, prerequisites = []
31+
<strong>输出:</strong>[0]
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<strong>提示:</strong>
36+
37+
<ul>
38+
<li><code>1 &lt;= numCourses &lt;= 2000</code></li>
39+
<li><code>0 &lt;= prerequisites.length &lt;= numCourses * (numCourses - 1)</code></li>
40+
<li><code>prerequisites[i].length == 2</code></li>
41+
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; numCourses</code></li>
42+
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
43+
<li>所有<code>[a<sub>i</sub>, b<sub>i</sub>]</code> <strong>互不相同</strong></li>
44+
</ul>
45+
46+
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>图</li><li>拓扑排序</li></div></div><br><div><li>👍 726</li><li>👎 0</li></div>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>给你一个有&nbsp;<code>n</code>&nbsp;个节点的 <strong>有向无环图(DAG)</strong>,请你找出所有从节点 <code>0</code>&nbsp;到节点 <code>n-1</code>&nbsp;的路径并输出(<strong>不要求按特定顺序</strong>)</p>
2+
3+
<p>
4+
<meta charset="UTF-8" />&nbsp;<code>graph[i]</code>&nbsp;是一个从节点 <code>i</code> 可以访问的所有节点的列表(即从节点 <code>i</code> 到节点&nbsp;<code>graph[i][j]</code>存在一条有向边)。</p>
5+
6+
<p>&nbsp;</p>
7+
8+
<p><strong>示例 1:</strong></p>
9+
10+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/28/all_1.jpg" /></p>
11+
12+
<pre>
13+
<strong>输入:</strong>graph = [[1,2],[3],[3],[]]
14+
<strong>输出:</strong>[[0,1,3],[0,2,3]]
15+
<strong>解释:</strong>有两条路径 0 -&gt; 1 -&gt; 3 和 0 -&gt; 2 -&gt; 3
16+
</pre>
17+
18+
<p><strong>示例 2:</strong></p>
19+
20+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/28/all_2.jpg" /></p>
21+
22+
<pre>
23+
<strong>输入:</strong>graph = [[4,3,1],[3,2,4],[3],[4],[]]
24+
<strong>输出:</strong>[[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
25+
</pre>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong>提示:</strong></p>
30+
31+
<ul>
32+
<li><code>n == graph.length</code></li>
33+
<li><code>2 &lt;= n &lt;= 15</code></li>
34+
<li><code>0 &lt;= graph[i][j] &lt; n</code></li>
35+
<li><code>graph[i][j] != i</code>(即不存在自环)</li>
36+
<li><code>graph[i]</code> 中的所有元素 <strong>互不相同</strong></li>
37+
<li>保证输入为 <strong>有向无环图(DAG)</strong></li>
38+
</ul>
39+
40+
<p>&nbsp;</p>
41+
42+
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>图</li><li>回溯</li></div></div><br><div><li>👍 352</li><li>👎 0</li></div>

0 commit comments

Comments
 (0)