Skip to content

Commit 9ee4037

Browse files
committed
feat: add solutions to lc problem: No.0733
No.0733.Flood Fill
1 parent b320360 commit 9ee4037

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

solution/0700-0799/0733.Flood Fill/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,69 @@ func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
226226
}
227227
```
228228

229+
### **TypeScript**
230+
231+
```ts
232+
function floodFill(
233+
image: number[][],
234+
sr: number,
235+
sc: number,
236+
newColor: number,
237+
): number[][] {
238+
const m = image.length;
239+
const n = image[0].length;
240+
const target = image[sr][sc];
241+
const dfs = (i: number, j: number) => {
242+
if (
243+
i < 0 ||
244+
i === m ||
245+
j < 0 ||
246+
j === n ||
247+
image[i][j] !== target ||
248+
image[i][j] === newColor
249+
) {
250+
return;
251+
}
252+
image[i][j] = newColor;
253+
dfs(i + 1, j);
254+
dfs(i - 1, j);
255+
dfs(i, j + 1);
256+
dfs(i, j - 1);
257+
};
258+
dfs(sr, sc);
259+
return image;
260+
}
261+
```
262+
263+
### **Rust**
264+
265+
```rust
266+
impl Solution {
267+
fn dfs(image: &mut Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32, target: i32) {
268+
if sr < 0 || sr == image.len() as i32 || sc < 0 || sc == image[0].len() as i32 {
269+
return;
270+
}
271+
let sr = sr as usize;
272+
let sc = sc as usize;
273+
if sr < 0 || image[sr][sc] == new_color || image[sr][sc] != target {
274+
return;
275+
}
276+
image[sr][sc] = new_color;
277+
let sr = sr as i32;
278+
let sc = sc as i32;
279+
Self::dfs(image, sr + 1, sc, new_color, target);
280+
Self::dfs(image, sr - 1, sc, new_color, target);
281+
Self::dfs(image, sr, sc + 1, new_color, target);
282+
Self::dfs(image, sr, sc - 1, new_color, target);
283+
}
284+
pub fn flood_fill(image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
285+
let target = image[sr as usize][sc as usize];
286+
Self::dfs(&mut image, sr, sc, new_color, target);
287+
image
288+
}
289+
}
290+
```
291+
229292
### **...**
230293

231294
```

solution/0700-0799/0733.Flood Fill/README_EN.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,69 @@ func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
242242
}
243243
```
244244

245+
### **TypeScript**
246+
247+
```ts
248+
function floodFill(
249+
image: number[][],
250+
sr: number,
251+
sc: number,
252+
newColor: number,
253+
): number[][] {
254+
const m = image.length;
255+
const n = image[0].length;
256+
const target = image[sr][sc];
257+
const dfs = (i: number, j: number) => {
258+
if (
259+
i < 0 ||
260+
i === m ||
261+
j < 0 ||
262+
j === n ||
263+
image[i][j] !== target ||
264+
image[i][j] === newColor
265+
) {
266+
return;
267+
}
268+
image[i][j] = newColor;
269+
dfs(i + 1, j);
270+
dfs(i - 1, j);
271+
dfs(i, j + 1);
272+
dfs(i, j - 1);
273+
};
274+
dfs(sr, sc);
275+
return image;
276+
}
277+
```
278+
279+
### **Rust**
280+
281+
```rust
282+
impl Solution {
283+
fn dfs(image: &mut Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32, target: i32) {
284+
if sr < 0 || sr == image.len() as i32 || sc < 0 || sc == image[0].len() as i32 {
285+
return;
286+
}
287+
let sr = sr as usize;
288+
let sc = sc as usize;
289+
if sr < 0 || image[sr][sc] == new_color || image[sr][sc] != target {
290+
return;
291+
}
292+
image[sr][sc] = new_color;
293+
let sr = sr as i32;
294+
let sc = sc as i32;
295+
Self::dfs(image, sr + 1, sc, new_color, target);
296+
Self::dfs(image, sr - 1, sc, new_color, target);
297+
Self::dfs(image, sr, sc + 1, new_color, target);
298+
Self::dfs(image, sr, sc - 1, new_color, target);
299+
}
300+
pub fn flood_fill(image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
301+
let target = image[sr as usize][sc as usize];
302+
Self::dfs(&mut image, sr, sc, new_color, target);
303+
image
304+
}
305+
}
306+
```
307+
245308
### **...**
246309

247310
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
fn dfs(image: &mut Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32, target: i32) {
3+
if sr < 0 || sr == image.len() as i32 || sc < 0 || sc == image[0].len() as i32 {
4+
return;
5+
}
6+
let sr = sr as usize;
7+
let sc = sc as usize;
8+
if sr < 0 || image[sr][sc] == new_color || image[sr][sc] != target {
9+
return;
10+
}
11+
image[sr][sc] = new_color;
12+
let sr = sr as i32;
13+
let sc = sc as i32;
14+
Self::dfs(image, sr + 1, sc, new_color, target);
15+
Self::dfs(image, sr - 1, sc, new_color, target);
16+
Self::dfs(image, sr, sc + 1, new_color, target);
17+
Self::dfs(image, sr, sc - 1, new_color, target);
18+
}
19+
pub fn flood_fill(image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
20+
let target = image[sr as usize][sc as usize];
21+
Self::dfs(&mut image, sr, sc, new_color, target);
22+
image
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function floodFill(
2+
image: number[][],
3+
sr: number,
4+
sc: number,
5+
newColor: number,
6+
): number[][] {
7+
const m = image.length;
8+
const n = image[0].length;
9+
const target = image[sr][sc];
10+
// 递归函数
11+
const dfs = (i: number, j: number) => {
12+
if (
13+
i < 0 ||
14+
i === m ||
15+
j < 0 ||
16+
j === n ||
17+
image[i][j] !== target ||
18+
image[i][j] === newColor
19+
) {
20+
return;
21+
}
22+
image[i][j] = newColor;
23+
dfs(i + 1, j);
24+
dfs(i - 1, j);
25+
dfs(i, j + 1);
26+
dfs(i, j - 1);
27+
};
28+
dfs(sr, sc);
29+
return image;
30+
}

0 commit comments

Comments
 (0)