Skip to content

Commit 95b83bf

Browse files
committed
solve problem Flood Fill
1 parent 84a7885 commit 95b83bf

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ All solutions will be accepted!
104104
|447|[Number Of Boomerangs](https://leetcode-cn.com/problems/number-of-boomerangs/description/)|[java/py/js](./algorithms/NumberOfBoomerangs)|Easy|
105105
|824|[Goat Latin](https://leetcode-cn.com/problems/goat-latin/description/)|[java/py/js](./algorithms/GoatLatin)|Easy|
106106
|206|[Reverse Linked List](https://leetcode-cn.com/problems/reverse-linked-list/description/)|[java/py/js](./algorithms/ReverseLinkedList)|Easy|
107+
|733|[Flood Fill](https://leetcode-cn.com/problems/flood-fill/description/)|[java/py/js](./algorithms/FloodFill)|Easy|
107108

108109
# Database
109110
|#|Title|Solution|Difficulty|

algorithms/FloodFill/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Flood Fill
2+
This problem is easy to solve by DFS

algorithms/FloodFill/Solution.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
3+
Map<String, Boolean> filledMap = new HashMap<String, Boolean>();
4+
List<Integer[]> stack = new ArrayList<Integer[]>();
5+
int color = image[sr][sc];
6+
7+
stack.add(new Integer[]{sr, sc});
8+
9+
while (stack.size() > 0) {
10+
Integer[] point = stack.get(stack.size() - 1);
11+
stack.remove(stack.size() - 1);
12+
String coordinate = String.valueOf(point[0]) + "X" + String.valueOf(point[1]);
13+
14+
if (filledMap.get(coordinate) == null) {
15+
filledMap.put(coordinate, true);
16+
} else {
17+
continue;
18+
}
19+
20+
if (point[0] >= 1 && image[point[0] - 1][point[1]] == color) {
21+
stack.add(new Integer[]{point[0] - 1, point[1]});
22+
}
23+
if (point[0] + 1 < image.length && image[point[0] + 1][point[1]] == color) {
24+
stack.add(new Integer[]{point[0] + 1, point[1]});
25+
}
26+
if (point[1] >= 1 && image[point[0]][point[1] - 1] == color) {
27+
stack.add(new Integer[]{point[0], point[1] - 1});
28+
}
29+
if (point[1] + 1 < image[0].length && image[point[0]][point[1] + 1] == color) {
30+
stack.add(new Integer[]{point[0], point[1] + 1});
31+
}
32+
image[point[0]][point[1]] = newColor;
33+
}
34+
35+
return image;
36+
}
37+
}

algorithms/FloodFill/solution.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[][]} image
3+
* @param {number} sr
4+
* @param {number} sc
5+
* @param {number} newColor
6+
* @return {number[][]}
7+
*/
8+
var floodFill = function(image, sr, sc, newColor) {
9+
let filledMap = {},
10+
stack = [[sr, sc]],
11+
color = image[sr][sc]
12+
13+
while (stack.length > 0) {
14+
let point = stack.pop(),
15+
coordinate = `${point[0]}X${point[1]}`
16+
17+
if (filledMap[coordinate] === undefined) {
18+
filledMap[coordinate] = true
19+
} else {
20+
continue
21+
}
22+
23+
if (point[0] >= 1 && image[point[0] - 1][point[1]] === color) {
24+
stack.push([point[0] - 1, point[1]])
25+
}
26+
if (point[0] + 1 < image.length && image[point[0] + 1][point[1]] === color) {
27+
stack.push([point[0] + 1, point[1]])
28+
}
29+
if (point[1] >= 1 && image[point[0]][point[1] - 1] === color) {
30+
stack.push([point[0], point[1] - 1])
31+
}
32+
if (point[1] + 1 < image[0].length && image[point[0]][point[1] + 1] === color) {
33+
stack.push([point[0], point[1] + 1])
34+
}
35+
image[point[0]][point[1]] = newColor
36+
}
37+
38+
return image
39+
};

algorithms/FloodFill/solution.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def floodFill(self, image, sr, sc, newColor):
3+
"""
4+
:type image: List[List[int]]
5+
:type sr: int
6+
:type sc: int
7+
:type newColor: int
8+
:rtype: List[List[int]]
9+
"""
10+
filled_map = {}
11+
stack = [[sr, sc]]
12+
color = image[sr][sc]
13+
14+
while len(stack) > 0:
15+
point = stack.pop()
16+
coordinate = str(point[0]) + 'X' + str(point[1])
17+
if filled_map.get(coordinate) == None:
18+
filled_map[coordinate] = True
19+
else:
20+
continue
21+
if point[0] >= 1 and image[point[0] - 1][point[1]] == color:
22+
stack.append([point[0] - 1, point[1]])
23+
if point[0] + 1 < len(image) and image[point[0] + 1][point[1]] == color:
24+
stack.append([point[0] + 1, point[1]])
25+
if point[1] >= 1 and image[point[0]][point[1] - 1] == color:
26+
stack.append([point[0], point[1] - 1])
27+
if point[1] + 1 < len(image[0]) and image[point[0]][point[1] + 1] == color:
28+
stack.append([point[0], point[1] + 1])
29+
image[point[0]][point[1]] = newColor
30+
return image
31+

0 commit comments

Comments
 (0)