Skip to content

Commit 40ceaa2

Browse files
committedNov 24, 2021
solves flood fil
1 parent a750a6b commit 40ceaa2

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed
 

Diff for: ‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
| 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary) | |
202202
| 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index) | [![Java](assets/java.png)](src/FindPivotIndex.java) [![Python](assets/python.png)](python/find_pivot_index.py) |
203203
| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers) | [![Java](assets/java.png)](src/SelfDividingNumbers.java) [![Python](assets/python.png)](python/self_dividing_number.py) |
204-
| 733 | [Flood Fill](https://leetcode.com/problems/flood-fill) | |
204+
| 733 | [Flood Fill](https://leetcode.com/problems/flood-fill) | [![Java](assets/java.png)](src/FloodFill.java) [![Python](assets/python.png)](python/) |
205205
| 734 | [Sentence Similarity](https://leetcode.com/problems/sentence-similarity) | |
206206
| 744 | [Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target) | |
207207
| 746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs) | |

Diff for: ‎python/flood_fill.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
6+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
7+
self.image = image
8+
self.height = len(image)
9+
self.width = len(image[0])
10+
self.newColor = newColor
11+
if image[sr][sc] != newColor:
12+
self.oldColor = image[sr][sc]
13+
self.colorPixel(row=sr, column=sc)
14+
return image
15+
16+
def colorPixel(self, row: int, column: int) -> None:
17+
if self.image[row][column] != self.newColor:
18+
self.image[row][column] = self.newColor
19+
if row - 1 >= 0 and self.image[row - 1][column] == self.oldColor: self.colorPixel(row - 1, column)
20+
if column + 1 < self.width and self.image[row][column + 1] == self.oldColor: self.colorPixel(row, column + 1)
21+
if row + 1 < self.height and self.image[row + 1][column] == self.oldColor: self.colorPixel(row + 1, column)
22+
if column - 1 >= 0 and self.image[row][column - 1] == self.oldColor: self.colorPixel(row, column - 1)

Diff for: ‎src/FloodFill.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class FloodFill {
2+
private int[][] image;
3+
private int height;
4+
private int width;
5+
6+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
7+
this.image = image;
8+
height = image.length;
9+
width = image[0].length;
10+
if (image[sr][sc] != newColor) {
11+
colorPixel(sr, sc, image[sr][sc], newColor);
12+
}
13+
return image;
14+
}
15+
16+
private void colorPixel(int row, int col, int oldColor, int newColor) {
17+
if (image[row][col] != newColor) {
18+
image[row][col] = newColor;
19+
if (row - 1 >= 0 && image[row - 1][col] == oldColor) colorPixel(row - 1, col, oldColor, newColor);
20+
if (col + 1 < width && image[row][col + 1] == oldColor) colorPixel(row, col + 1, oldColor, newColor);
21+
if (row + 1 < height && image[row + 1][col] == oldColor) colorPixel(row + 1, col, oldColor, newColor);
22+
if (col - 1 >= 0 && image[row][col - 1] == oldColor) colorPixel(row, col - 1, oldColor, newColor);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.