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

Commit ab90e74

Browse files
Merge pull request youngyangyang04#2246 from fwqaaq/fwqaaq-patch-shen
Update 0200.岛屿数量.深搜版.md about
2 parents 1bf9631 + b751d1f commit ab90e74

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎problems/0200.岛屿数量.深搜版.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,42 @@ class Solution:
279279
return result
280280
```
281281

282+
Rust:
283+
284+
285+
```rust
286+
impl Solution {
287+
const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)];
288+
pub fn num_islands(grid: Vec<Vec<char>>) -> i32 {
289+
let mut visited = vec![vec![false; grid[0].len()]; grid.len()];
290+
let mut res = 0;
291+
for (i, chars) in grid.iter().enumerate() {
292+
for (j, &c) in chars.iter().enumerate() {
293+
if !visited[i][j] && c == '1' {
294+
res += 1;
295+
Self::dfs(&grid, &mut visited, (i as i32, j as i32));
296+
}
297+
}
298+
}
299+
res
300+
}
301+
302+
pub fn dfs(grid: &Vec<Vec<char>>, visited: &mut Vec<Vec<bool>>, (x, y): (i32, i32)) {
303+
for (dx, dy) in Self::DIRECTIONS {
304+
let (nx, ny) = (x + dx, y + dy);
305+
if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 {
306+
continue;
307+
}
308+
let (nx, ny) = (nx as usize, ny as usize);
309+
if grid[nx][ny] == '1' && !visited[nx][ny] {
310+
visited[nx][ny] = true;
311+
Self::dfs(grid, visited, (nx as i32, ny as i32));
312+
}
313+
}
314+
}
315+
}
316+
```
317+
282318
<p align="center">
283319
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
284320
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

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