From 8887adbb109acd58ac65bea198d1dc287d99db42 Mon Sep 17 00:00:00 2001 From: limuxuale0927 Date: 2023年7月14日 21:52:24 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200200.=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E6=95=B0=E9=87=8F.=E5=B9=BF=E6=90=9C=E7=89=88.md=20Py?= =?UTF-8?q?thon3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.345円271円277円346円220円234円347円211円210円.md" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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 39af9f50a3..4a990a445b 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" @@ -196,6 +196,41 @@ 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)] # 四个方向 + ans = 0 + + def bfs(x, y): + q = deque() + q.append([x, y]) + visited[x][y] = True + while q: + curx, cury = q.popleft() + for d in dirs: + nextx = curx + d[0] + nexty = cury + d[1] + if nextx < 0 or nextx>= m or nexty < 0 or nexty>= n: # 越界了,直接跳过 + continue + if visited[nextx][nexty] == False and grid[nextx][nexty] == "1": + q.append([nextx, nexty]) + visited[nextx][nexty] = True # 只要加入队列立刻标记 + + for i in range(m): + for j in range(n): + if grid[i][j] == "1" and not visited[i][j]: + ans += 1 # 遇到没访问过的陆地,+1 + bfs(i, j) # 将与其链接的陆地都标记上 true + + return ans +``` +

From 9b5c5ebeea85b133514ab42c0218b79e13599b45 Mon Sep 17 00:00:00 2001 From: limuxuale0927 Date: 2023年7月14日 22:07:13 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200200.=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E6=95=B0=E9=87=8F.=E6=B7=B1=E6=90=9C=E7=89=88.md=20Py?= =?UTF-8?q?thon3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.346円267円261円346円220円234円347円211円210円.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) 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 +``` +

From 5794b17ab4ccb5da0411896eda509c503014d7d6 Mon Sep 17 00:00:00 2001 From: TonySu Date: 2023年7月16日 22:00:05 +0800 Subject: [PATCH 3/4] =?UTF-8?q?Update=200039.=E7=BB=84=E5=90=88=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md,=20=E4=BF=AE=E5=A4=8DPython=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E4=B8=AAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python 版本的回溯剪枝版本一当中, break 会终止整个for 循环,从而提前终止搜索,应该改成continue --- .../0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 4d9466c3c0..e09a44e496 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) From 826554682f465da29414c4ad6aa9ba391a54e05a Mon Sep 17 00:00:00 2001 From: fwqaaq Date: 2023年7月18日 16:51:54 +0800 Subject: [PATCH 4/4] =?UTF-8?q?Update=200674.=E6=9C=80=E9=95=BF=E8=BF=9E?= =?UTF-8?q?=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22345円242円236円345円272円217円345円210円227円.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0674.346円234円200円351円225円277円350円277円236円347円273円255円351円200円222円345円242円236円345円272円217円345円210円227円.md" "b/problems/0674.346円234円200円351円225円277円350円277円236円347円273円255円351円200円222円345円242円236円345円272円217円345円210円227円.md" index 8cc270ec64..58365df3a7 100644 --- "a/problems/0674.346円234円200円351円225円277円350円277円236円347円273円255円351円200円222円345円242円236円345円272円217円345円210円227円.md" +++ "b/problems/0674.346円234円200円351円225円277円350円277円236円347円273円255円351円200円222円345円242円236円345円272円217円345円210円227円.md" @@ -303,6 +303,9 @@ func findLengthOfLCIS(nums []int) int { ``` Rust: + +>动态规划 + ```rust pub fn find_length_of_lcis(nums: Vec) -> i32 { if nums.is_empty() { @@ -320,6 +323,25 @@ pub fn find_length_of_lcis(nums: Vec) -> i32 { } ``` +> 贪心 + +```rust +impl Solution { + pub fn find_length_of_lcis(nums: Vec) -> i32 { + let (mut res, mut count) = (1, 1); + for i in 1..nums.len() { + if nums[i]> nums[i - 1] { + count += 1; + res = res.max(count); + continue; + } + count = 1; + } + res + } +} +``` + Javascript: > 动态规划:

AltStyle によって変換されたページ (->オリジナル) /