-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.js
80 lines (71 loc) · 2.72 KB
/
Solution.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* @param {number[][]} image
* @param {number} sr
* @param {number} sc
* @param {number} newColor
* @return {number[][]}
*/
var floodFill = function(image, sr, sc, newColor) {
var stack1 = [], stack2 = [], pixel, rMax, cMax, color;
rMax = image.length;
if (rMax) cMax = image[0].length;
stack1.push([sr, sc]);
color = image[sr][sc];
while(stack1.length) {
pixel = stack1.pop();
stack2.push(pixel);
if (pixel[0] - 1 >= 0 && image[pixel[0] - 1][pixel[1]] === color) stack1.push([pixel[0] - 1, pixel[1]]);
if (pixel[0] + 1 < rMax && image[pixel[0] + 1][pixel[1]] === color) stack1.push([pixel[0] + 1, pixel[1]]);
if (pixel[1] - 1 >= 0 && image[pixel[0]][pixel[1] - 1] === color) stack1.push([pixel[0], pixel[1] - 1]);
if (pixel[1] + 1 < cMax && image[pixel[0]][pixel[1] + 1] === color) stack1.push([pixel[0], pixel[1] + 1]);
image[pixel[0]][pixel[1]] = -1;
}
while(stack2.length) {
pixel = stack2.pop();
image[pixel[0]][pixel[1]] = newColor;
};
return image;
};
/**
* @param {number[][]} image
* @param {number} sr
* @param {number} sc
* @param {number} newColor
* @return {number[][]}
*/
var floodFill = function(image, sr, sc, newColor) {
var color = image[sr][sc], rMax = image.length, cMax = image[0].length;
function dfs(image, r, c, color, newColor) {
if (image[r][c] == color) {
image[r][c] = newColor;
if (r >= 1) dfs(image, r-1, c, color, newColor);
if (c >= 1) dfs(image, r, c-1, color, newColor);
if (r+1 < rMax) dfs(image, r+1, c, color, newColor);
if (c+1 < cMax) dfs(image, r, c+1, color, newColor);
}
}
if (color != newColor) dfs(image, sr, sc, color, newColor);
return image;
};
/**
* @param {number[][]} image
* @param {number} sr
* @param {number} sc
* @param {number} newColor
* @return {number[][]}
*/
var floodFill = function(image, sr, sc, newColor) {
var stack = [], pixel, rMax = image.length, cMax = image[0].length, color;
stack.push([sr, sc]);
color = image[sr][sc];
while(stack.length) {
pixel = stack.pop();
if (image[pixel[0]][pixel[1]] === newColor) continue;
if (pixel[0] - 1 >= 0 && image[pixel[0] - 1][pixel[1]] === color) stack.push([pixel[0] - 1, pixel[1]]);
if (pixel[0] + 1 < rMax && image[pixel[0] + 1][pixel[1]] === color) stack.push([pixel[0] + 1, pixel[1]]);
if (pixel[1] - 1 >= 0 && image[pixel[0]][pixel[1] - 1] === color) stack.push([pixel[0], pixel[1] - 1]);
if (pixel[1] + 1 < cMax && image[pixel[0]][pixel[1] + 1] === color) stack.push([pixel[0], pixel[1] + 1]);
image[pixel[0]][pixel[1]] = newColor;
}
return image;
};