Skip to content

Commit b808123

Browse files
committed
feat: add rust solution to lc problem: No.0695
No.0695.Max Area of Island
1 parent 9ee4037 commit b808123

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

solution/0600-0699/0695.Max Area of Island/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,41 @@ func max(a, b int) int {
507507
}
508508
```
509509

510+
### **Rust**
511+
512+
DFS:
513+
514+
```rust
515+
impl Solution {
516+
fn dfs(grid: &mut Vec<Vec<i32>>, i: usize, j: usize) -> i32 {
517+
if i == grid.len() || j == grid[0].len() || grid[i][j] == 0 {
518+
return 0;
519+
}
520+
grid[i][j] = 0;
521+
let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1);
522+
if i != 0 {
523+
res += Self::dfs(grid, i - 1, j)
524+
}
525+
if j != 0 {
526+
res += Self::dfs(grid, i, j - 1)
527+
}
528+
res
529+
}
530+
531+
pub fn max_area_of_island(mut grid: Vec<Vec<i32>>) -> i32 {
532+
let m = grid.len();
533+
let n = grid[0].len();
534+
let mut res = 0;
535+
for i in 0..m {
536+
for j in 0..n {
537+
res = res.max(Self::dfs(&mut grid, i, j))
538+
}
539+
}
540+
res
541+
}
542+
}
543+
```
544+
510545
### **...**
511546

512547
```

solution/0600-0699/0695.Max Area of Island/README_EN.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,41 @@ func max(a, b int) int {
442442
}
443443
```
444444

445+
### **Rust**
446+
447+
DFS:
448+
449+
```rust
450+
impl Solution {
451+
fn dfs(grid: &mut Vec<Vec<i32>>, i: usize, j: usize) -> i32 {
452+
if i == grid.len() || j == grid[0].len() || grid[i][j] == 0 {
453+
return 0;
454+
}
455+
grid[i][j] = 0;
456+
let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1);
457+
if i != 0 {
458+
res += Self::dfs(grid, i - 1, j)
459+
}
460+
if j != 0 {
461+
res += Self::dfs(grid, i, j - 1)
462+
}
463+
res
464+
}
465+
466+
pub fn max_area_of_island(mut grid: Vec<Vec<i32>>) -> i32 {
467+
let m = grid.len();
468+
let n = grid[0].len();
469+
let mut res = 0;
470+
for i in 0..m {
471+
for j in 0..n {
472+
res = res.max(Self::dfs(&mut grid, i, j))
473+
}
474+
}
475+
res
476+
}
477+
}
478+
```
479+
445480
### **...**
446481

447482
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
fn dfs(grid: &mut Vec<Vec<i32>>, i: usize, j: usize) -> i32 {
3+
if i == grid.len() || j == grid[0].len() || grid[i][j] == 0 {
4+
return 0;
5+
}
6+
grid[i][j] = 0;
7+
let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1);
8+
if i != 0 {
9+
res += Self::dfs(grid, i - 1, j)
10+
}
11+
if j != 0 {
12+
res += Self::dfs(grid, i, j - 1)
13+
}
14+
res
15+
}
16+
17+
pub fn max_area_of_island(mut grid: Vec<Vec<i32>>) -> i32 {
18+
let m = grid.len();
19+
let n = grid[0].len();
20+
let mut res = 0;
21+
for i in 0..m {
22+
for j in 0..n {
23+
res = res.max(Self::dfs(&mut grid, i, j))
24+
}
25+
}
26+
res
27+
}
28+
}

0 commit comments

Comments
 (0)