Skip to content

Commit fcc73a0

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 2.20 MB (Top 79.31%)
1 parent 5b78f07 commit fcc73a0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 2.20 MB (Top 79.31%)
2+
3+
impl Solution {
4+
pub fn flood_fill(mut image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
5+
let old_color = image[sr as usize][sc as usize];
6+
if old_color == new_color {
7+
return image;
8+
}
9+
let mut stack = vec![(sr as usize, sc as usize)];
10+
while let Some((i, j)) = stack.pop() {
11+
if image[i][j] != old_color {
12+
continue;
13+
}
14+
image[i][j] = new_color;
15+
if i > 0 {
16+
stack.push((i - 1, j));
17+
}
18+
if i < image.len() - 1 {
19+
stack.push((i + 1, j))
20+
}
21+
if j > 0 {
22+
stack.push((i, j - 1));
23+
}
24+
if j < image[0].len() - 1 {
25+
stack.push((i, j + 1));
26+
}
27+
}
28+
image
29+
}
30+
}

0 commit comments

Comments
 (0)