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 90ce460

Browse files
authored
Update 1020.飞地的数量.md
1 parent 49e1ab2 commit 90ce460

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎problems/1020.飞地的数量.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,44 @@ class Solution:
491491
return ans
492492
```
493493

494+
### Rust
495+
496+
dfs:
497+
498+
```rust
499+
impl Solution {
500+
const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)];
501+
502+
pub fn num_enclaves(mut grid: Vec<Vec<i32>>) -> i32 {
503+
for i in 0..grid.len() {
504+
for j in 0..grid[0].len() {
505+
if (i == 0 || i == grid.len() - 1 || j == 0 || j == grid[0].len() - 1)
506+
&& grid[i][j] == 1
507+
{
508+
Self::dfs(&mut grid, (i as i32, j as i32));
509+
}
510+
}
511+
}
512+
grid.iter()
513+
.map(|nums| nums.iter().filter(|&&num| num == 1).count() as i32)
514+
.sum()
515+
}
516+
517+
pub fn dfs(grid: &mut [Vec<i32>], (x, y): (i32, i32)) {
518+
grid[x as usize][y as usize] = 0;
519+
for (dx, dy) in Self::DIRECTIONS {
520+
let (nx, ny) = (x + dx, y + dy);
521+
if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 {
522+
continue;
523+
}
524+
if grid[nx as usize][ny as usize] == 0 {
525+
continue;
526+
}
527+
Self::dfs(grid, (nx, ny));
528+
}
529+
}
530+
}
531+
```
494532

495533

496534
## 类似题目

0 commit comments

Comments
(0)

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