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 d02c6a7

Browse files
authored
Update 0695.岛屿的最大面积.md about rust
1 parent eedb042 commit d02c6a7

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

‎problems/0695.岛屿的最大面积.md‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,51 @@ impl Solution {
439439
}
440440
```
441441

442+
dfs: 版本二
443+
444+
```rust
445+
impl Solution {
446+
const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)];
447+
448+
pub fn max_area_of_island(grid: Vec<Vec<i32>>) -> i32 {
449+
let mut visited = vec![vec![false; grid[0].len()]; grid.len()];
450+
451+
let mut res = 0;
452+
for (i, nums) in grid.iter().enumerate() {
453+
for (j, &num) in nums.iter().enumerate() {
454+
if !visited[i][j] && num == 1 {
455+
let mut count = 0;
456+
Self::dfs(&grid, &mut visited, (i as i32, j as i32), &mut count);
457+
res = res.max(count);
458+
}
459+
}
460+
}
461+
462+
res
463+
}
464+
465+
pub fn dfs(
466+
grid: &Vec<Vec<i32>>,
467+
visited: &mut [Vec<bool>],
468+
(x, y): (i32, i32),
469+
count: &mut i32,
470+
) {
471+
if visited[x as usize][y as usize] || grid[x as usize][y as usize] == 0 {
472+
return;
473+
}
474+
visited[x as usize][y as usize] = true;
475+
*count += 1;
476+
for (dx, dy) in Self::DIRECTIONS {
477+
let (nx, ny) = (x + dx, y + dy);
478+
if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 {
479+
continue;
480+
}
481+
Self::dfs(grid, visited, (nx, ny), count);
482+
}
483+
}
484+
}
485+
```
486+
442487
<p align="center">
443488
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
444489
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

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