diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md" index 6c6650ad00..f265f41e47 100755 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -305,3 +305,47 @@ class Solution { } ``` +### Python + +```python +class Solution: + def totalNQueens(self, n: int) -> int: + count = 0 + chessboard = [['.'] * n for _ in range(n)] # 初始化棋盘 + + def is_valid(row: int, col: int) -> bool: + # 检查列是否有皇后 + for i in range(row): + if chessboard[i][col] == 'Q': + return False + # 检查45度角(左上)是否有皇后 + i, j = row - 1, col - 1 + while i >= 0 and j >= 0: + if chessboard[i][j] == 'Q': + return False + i -= 1 + j -= 1 + # 检查135度角(右上)是否有皇后 + i, j = row - 1, col + 1 + while i >= 0 and j < n: + if chessboard[i][j] == 'Q': + return False + i -= 1 + j += 1 + return True + + def backtracking(row: int) -> None: + nonlocal count + if row == n: + count += 1 + return + for col in range(n): + if is_valid(row, col): + chessboard[row][col] = 'Q' # 放置皇后 + backtracking(row + 1) + chessboard[row][col] = '.' # 回溯 + + backtracking(0) + return count +``` + diff --git "a/problems/0207.\350\257\276\347\250\213\350\241\250.md" "b/problems/0207.\350\257\276\347\250\213\350\241\250.md" index f992c72b89..7a48777a8e 100644 --- "a/problems/0207.\350\257\276\347\250\213\350\241\250.md" +++ "b/problems/0207.\350\257\276\347\250\213\350\241\250.md" @@ -57,3 +57,32 @@ public: } }; ``` + +## 其他语言版本 + +### Python + +```python +from collections import deque +from typing import List + +class Solution: + def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: + in_degree = [0] * numCourses # 每门课程的入度 + graph = [[] for _ in range(numCourses)] # 邻接表:pre -> [后续课程] + for cur, pre in prerequisites: + # 上 cur 之前必须先上 pre,即 pre -> cur + graph[pre].append(cur) + in_degree[cur] += 1 + + que = deque([i for i in range(numCourses) if in_degree[i] == 0]) # 入度为0的课程入队 + count = 0 + while que: + cur = que.popleft() + count += 1 # 已修课程数 +1 + for nxt in graph[cur]: + in_degree[nxt] -= 1 # 后续课程入度 -1 + if in_degree[nxt] == 0: + que.append(nxt) + return count == numCourses +``` diff --git "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" index 9991249f5a..770fe2f148 100755 --- "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" +++ "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" @@ -74,4 +74,23 @@ public: }; ``` +## 其他语言版本 + +### Python + +```python +class Solution: + def findCenter(self, edges: List[List[int]]) -> int: + # 统计各个节点的度,只有中心节点的度会大于1 + degree = {} + for u, v in edges: + degree[u] = degree.get(u, 0) + 1 + degree[v] = degree.get(v, 0) + 1 + if degree[u] > 1: + return u + if degree[v] > 1: + return v + return -1 +``` +