-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path733-Flood-Fill.js
More file actions
31 lines (27 loc) · 801 Bytes
/
733-Flood-Fill.js
File metadata and controls
31 lines (27 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 733. Flood Fill [Easy]
// https://leetcode.com/problems/flood-fill/
/**
* @param {number[][]} image
* @param {number} sr
* @param {number} sc
* @param {number} newColor
* @return {number[][]}
*/
const floodFill = (image, sr, sc, newColor, currentColor = image[sr][sc]) => {
if (
!image ||
sr < 0 ||
sc < 0 ||
sr >= image.length ||
sc >= image[0].length ||
image[sr][sc] !== currentColor ||
image[sr][sc] === newColor
)
return image;
image[sr][sc] = newColor;
floodFill(image, sr + 1, sc, newColor, currentColor);
floodFill(image, sr - 1, sc, newColor, currentColor);
floodFill(image, sr, sc + 1, newColor, currentColor);
floodFill(image, sr, sc - 1, newColor, currentColor);
return image;
};