Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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
pull merged 9 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions problems/0200.岛屿数量.广搜版.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -197,38 +197,47 @@ class Solution {
}
```

Python:

### Python
BFS solution
```python
class Solution:
def __init__(self):
self.dirs = [[0, 1], [1, 0], [-1, 0], [0, -1]]

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 # 只要加入队列立刻标记

m = len(grid)
n = len(grid[0])
visited = [[False]*n for _ in range(m)]
res = 0
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
if visited[i][j] == False and grid[i][j] == '1':
res += 1
self.bfs(grid, i, j, visited) # Call bfs within this condition
return res

def bfs(self, grid, i, j, visited):
q = deque()
q.append((i,j))
visited[i][j] = True
while q:
x, y = q.popleft()
for k in range(4):
next_i = x + self.dirs[k][0]
next_j = y + self.dirs[k][1]

if next_i < 0 or next_i >= len(grid):
continue
if next_j < 0 or next_j >= len(grid[0]):
continue
if visited[next_i][next_j]:
continue
if grid[next_i][next_j] == '0':
continue
q.append((next_i, next_j))
visited[next_i][next_j] = True

return ans
```

<p align="center">
Expand Down
24 changes: 23 additions & 1 deletion problems/0674.最长连续递增序列.md
View file Open in desktop
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

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