Skip to content

Commit 43f7050

Browse files
committed
733
1 parent 441874d commit 43f7050

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

DFS/traditionalDFS/733.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Flood Fill
2+
3+
#### Description
4+
5+
[link](https://leetcode.com/problems/flood-fill/description/)
6+
7+
---
8+
9+
#### Solution
10+
11+
- See Code
12+
13+
---
14+
15+
#### Code
16+
17+
> 最坏情况:O(n^2)
18+
19+
```python
20+
class Solution:
21+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
22+
rows, cols, orig_color = len(image), len(image[0]), image[sr][sc]
23+
def traverse(row, col):
24+
if (not (0 <= row < rows and 0 <= col < cols)) or image[row][col] != orig_color:
25+
return
26+
image[row][col] = newColor
27+
[traverse(row + x, col + y) for (x, y) in ((0, 1), (1, 0), (0, -1), (-1, 0))]
28+
if orig_color != newColor:
29+
traverse(sr, sc)
30+
return image
31+
```

0 commit comments

Comments
 (0)