Flood Fill LeetCode solution in Rust

1
impl Solution {
2
pub fn flood_fill(mut image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
3
let old_color = image[sr as usize][sc as usize];
4
if old_color == new_color {
5
return image;
6
}
7
let mut stack = vec![(sr as usize, sc as usize)];
8
while let Some((i, j)) = stack.pop() {
9
if image[i][j] != old_color {
10
continue;
11
}
12
image[i][j] = new_color;
13
if i > 0 {
14
stack.push((i - 1, j));
15
}
16
if i < image.len() - 1 {
17
stack.push((i + 1, j))
18
}
19
if j > 0 {
20
stack.push((i, j - 1));
21
}
22
if j < image[0].len() - 1 {
23
stack.push((i, j + 1));
24
}
25
}
26
image
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0