Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b69330f

Browse files
committed
day-eleven
1 parent f27d953 commit b69330f

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
- Day 7: [Cousins in Binary Tree](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-seven/index.ts) :hushed:
1111
- Day 8: [check Straight Line](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-eight/index.ts) :grin:
1212
- Day 9: [Is Perfect Square](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-nine/index.ts) :alien:
13-
- Day 10: [Find Town Judge](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-ten/index.ts) :alien:
13+
- Day 10: [Find Town Judge](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-ten/index.ts) :poop:
14+
- Day 11: [Flood Fill](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-eleven/index.ts) :raising_hand:

src/may/day-eleven/index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param {number[][]} image
3+
* @param {number} sr
4+
* @param {number} sc
5+
* @param {number} newColor
6+
* @return {number[][]}
7+
*/
8+
export const floodFill = function (image: number[][], sr: number, sc: number, newColor: number, startPixel = image[sr][sc]): number[][] {
9+
// Exchange Start and handle exception
10+
if (sr < 0 || sc < 0 || sr >= image.length || sc >= image[sr].length || image[sr][sc] !== startPixel || image[sr][sc] === newColor) return image;
11+
12+
// Exchange Pixel
13+
image[sr][sc] = newColor;
14+
// check up down left right
15+
floodFill(image, sr - 1, sc, newColor, startPixel);
16+
floodFill(image, sr + 1, sc, newColor, startPixel);
17+
floodFill(image, sr, sc - 1, newColor, startPixel);
18+
floodFill(image, sr, sc + 1, newColor, startPixel);
19+
20+
return image;
21+
};
22+
23+
const a = floodFill(
24+
[
25+
[1, 1, 1],
26+
[1, 1, 0],
27+
[1, 0, 1],
28+
],
29+
1,
30+
1,
31+
2,
32+
);
33+
34+
const b = floodFill(
35+
[
36+
[0, 0, 0],
37+
[0, 0, 0],
38+
],
39+
0,
40+
0,
41+
2,
42+
);
43+
44+
console.log('a', a);
45+
console.log('b', b);

0 commit comments

Comments
 (0)