Skip to content

Commit

Permalink
create 0200-number-of-islands.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrt1n committed Aug 11, 2023
1 parent 704285e commit 48d3aad
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions rust/0200-number-of-islands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
impl Solution {
pub fn num_islands(grid: Vec<Vec<char>>) -> i32 {
fn dfs(grid: &mut Vec<Vec<char>>, x: i32, y: i32) {
if x < 0
|| y < 0
|| x >= grid.len() as i32
|| y >= grid[0].len() as i32
|| grid[x as usize][y as usize] == '0'
{
return;
}

grid[x as usize][y as usize] = '0';

let directions: [(i32, i32); 4] = [(0, 1), (1, 0), (0, -1), (-1, 0)];

for (add_x, add_y) in directions {
dfs(grid, x + add_x, y + add_y);
}
}

let mut count = 0;
let mut new_grid = grid.clone();

for x in 0..grid.len() {
for y in 0..grid[0].len() {
if new_grid[x][y] == '1' {
count += 1;
dfs(&mut new_grid, x as i32, y as i32);
}
}
}

count
}
}

0 comments on commit 48d3aad

Please sign in to comment.