Skip to content

[pull] master from youngyangyang04:master #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 31, 2023
2 changes: 1 addition & 1 deletion problems/0039.组合总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions problems/0200.岛屿数量.广搜版.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class Solution {
}
```


### Python
BFS solution
```python
Expand Down Expand Up @@ -236,6 +237,7 @@ class Solution:
continue
q.append((next_i, next_j))
visited[next_i][next_j] = True

```

<p align="center">
Expand Down
61 changes: 61 additions & 0 deletions problems/0200.岛屿数量.深搜版.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down
24 changes: 23 additions & 1 deletion problems/0674.最长连续递增序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,9 @@ func findLengthOfLCIS(nums []int) int {
}
```

### Rust:

### Rust:
>动态规划
```rust
pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
if nums.is_empty() {
Expand All @@ -321,6 +322,27 @@ pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
}
```


> 贪心

```rust
impl Solution {
pub fn find_length_of_lcis(nums: Vec<i32>) -> 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:

> 动态规划:
Expand Down