diff --git "a/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" "b/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" index 2a36ce5adb..cdf33c5812 100644 --- "a/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" +++ "b/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" @@ -311,7 +311,7 @@ class Solution: for i in range(startIndex, len(candidates)): if total + candidates[i]> target: - break + continue total += candidates[i] path.append(candidates[i]) self.backtracking(candidates, target, total, i, path, result) diff --git "a/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" "b/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" index cd3ae70d71..5b9d90aa31 100644 --- "a/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" +++ "b/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" @@ -197,6 +197,7 @@ class Solution { } ``` + ### Python BFS solution ```python @@ -236,6 +237,7 @@ class Solution: continue q.append((next_i, next_j)) visited[next_i][next_j] = True + ```
diff --git "a/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.346円267円261円346円220円234円347円211円210円.md" "b/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.346円267円261円346円220円234円347円211円210円.md" index c30ace1999..f610e32330 100644 --- "a/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.346円267円261円346円220円234円347円211円210円.md" +++ "b/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.346円267円261円346円220円234円347円211円210円.md" @@ -218,6 +218,67 @@ class Solution { } } ``` + +Python: + +```python +# 版本一 +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + result = 0 + + def dfs(x, y): + for d in dirs: + nextx = x + d[0] + nexty = y + d[1] + if nextx < 0 or nextx>= m or nexty < 0 or nexty>= n: # 越界了,直接跳过 + continue + if not visited[nextx][nexty] and grid[nextx][nexty] == '1': # 没有访问过的同时是陆地的 + visited[nextx][nexty] = True + dfs(nextx, nexty) + + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == '1': + visited[i][j] = True + result += 1 # 遇到没访问过的陆地,+1 + dfs(i, j) # 将与其链接的陆地都标记上 true + + return result +``` + +```python +# 版本二 +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + result = 0 + + def dfs(x, y): + if visited[x][y] or grid[x][y] == '0': + return # 终止条件:访问过的节点 或者 遇到海水 + visited[x][y] = True + for d in dirs: + nextx = x + d[0] + nexty = y + d[1] + if nextx < 0 or nextx>= m or nexty < 0 or nexty>= n: # 越界了,直接跳过 + continue + dfs(nextx, nexty) + + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == '1': + result += 1 # 遇到没访问过的陆地,+1 + dfs(i, j) # 将与其链接的陆地都标记上 true + + return result +``` +