Skip to content

Commit a348ec1

Browse files
author
谭佳胜
committed
Flood Fill
1 parent aef601e commit a348ec1

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

733. Flood Fill/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const floodFill = function(image, sr, sc, newColor, lastColor) {
2+
if (image[sr] === undefined || image[sr][sc] === undefined) return;
3+
4+
if (lastColor === undefined) {
5+
lastColor = image[sr][sc];
6+
image[sr][sc] = newColor;
7+
} else if (image[sr][sc] === lastColor && lastColor !== newColor) {
8+
image[sr][sc] = newColor;
9+
} else {
10+
return;
11+
}
12+
13+
floodFill(image, sr, sc + 1, newColor, lastColor);
14+
floodFill(image, sr + 1, sc, newColor, lastColor);
15+
floodFill(image, sr, sc - 1, newColor, lastColor);
16+
floodFill(image, sr - 1, sc, newColor, lastColor);
17+
18+
return image;
19+
};

733. Flood Fill/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dfs算法

0 commit comments

Comments
 (0)