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

Commit f39308b

Browse files
Merge branch 'master' into unique-paths
2 parents c515f00 + 81d5f3d commit f39308b

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

FloodFill.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
https://leetcode.com/problems/flood-fill/description/
3+
4+
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).
5+
6+
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
7+
8+
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
9+
10+
At the end, return the modified image.
11+
12+
Example 1:
13+
Input:
14+
image = [[1,1,1],[1,1,0],[1,0,1]]
15+
sr = 1, sc = 1, newColor = 2
16+
Output: [[2,2,2],[2,2,0],[2,0,1]]
17+
Explanation:
18+
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
19+
by a path of the same color as the starting pixel are colored with the new color.
20+
Note the bottom corner is not colored 2, because it is not 4-directionally connected
21+
to the starting pixel.
22+
Note:
23+
24+
The length of image and image[0] will be in the range [1, 50].
25+
The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
26+
The value of each color in image[i][j] and newColor will be an integer in [0, 65535].
27+
28+
*/
29+
30+
var floodFill = function(image, sr, sc, newColor) {
31+
var oldColor = image[sr][sc];
32+
33+
if(newColor == oldColor) {
34+
return image;
35+
}
36+
37+
image[sr][sc] = newColor;
38+
39+
if(sr > 0 && image[sr - 1][sc] == oldColor) {
40+
floodFill(image, sr - 1, sc, newColor); //Left
41+
}
42+
if(sc > 0 && image[sr][sc - 1] == oldColor) {
43+
floodFill(image, sr, sc - 1, newColor); //Up
44+
}
45+
if(sr < image.length - 1 && image[sr + 1][sc] == oldColor) {
46+
floodFill(image, sr + 1, sc, newColor); //Down
47+
}
48+
if(sc < image[0].length - 1 && image[sr][sc + 1] == oldColor) {
49+
floodFill(image, sr, sc + 1, newColor); // Right
50+
}
51+
return image;
52+
};
53+
54+
55+
function main() {
56+
console.log(floodFill([[1,1,1],[1,1,0],[1,0,1]], 1, 1, 2))
57+
}
58+
59+
module.exports.main = main;

Main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ var permutationWithoutDuplicates = require("./PermutationsWithoutDuplicates.js")
33
var permutationWithDuplicates = require("./PermutationsWithDuplicates.js");
44
var subsets = require("./Subsets.js");
55
var uniquePaths = require("./UniquePaths.js");
6+
var floodFill = require("./FloodFill.js")
67

78
// Invocation
89

910
// permutationWithoutDuplicates.main();
1011
// permutationWithDuplicates.main();
1112
// subsets.main();
12-
// uniquePaths.main();
13+
// uniquePaths.main();
14+
// floodFill.main();

0 commit comments

Comments
 (0)