|
1 |
| -[xxxx][title] |
| 1 | +[Flood Fill ][title] |
2 | 2 |
|
3 | 3 | ## Description
|
4 |
| -// 抄题目 |
5 | 4 |
|
| 5 | +An `image` is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). |
6 | 6 |
|
7 |
| -**Example:** |
| 7 | +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. |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +At the end, return the modified image. |
| 12 | + |
| 13 | +**Example 1:** |
8 | 14 |
|
9 | 15 | ```
|
10 |
| -// 抄Example |
| 16 | +Input: |
| 17 | +image = [[1,1,1],[1,1,0],[1,0,1]] |
| 18 | +sr = 1, sc = 1, newColor = 2 |
| 19 | +Output: [[2,2,2],[2,2,0],[2,0,1]] |
| 20 | +Explanation: |
| 21 | +From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected |
| 22 | +by a path of the same color as the starting pixel are colored with the new color. |
| 23 | +Note the bottom corner is not colored 2, because it is not 4-directionally connected |
| 24 | +to the starting pixel. |
11 | 25 | ```
|
12 | 26 |
|
13 | 27 | **Note:**
|
14 |
| -// Note |
15 | 28 |
|
16 |
| -**Tags:** // tags |
| 29 | +The length of `image` and `image[0]` will be in the range `[1, 50]`. |
17 | 30 |
|
| 31 | +The given starting pixel will satisfy `0 <= sr < image.length` and `0 <= sc < image[0].length`. |
18 | 32 |
|
19 |
| -## 思路 1 |
20 |
| -// 贴一些关键代码,说一些解题思路 |
21 |
| -// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面) |
22 |
| -```java |
| 33 | +The value of each color in `image[i][j]` and `newColor` will be an integer in `[0, 65535]`. |
23 | 34 |
|
24 |
| -``` |
25 |
| -```javascript |
| 35 | +**Tags:** [Depth-first Search](https://leetcode.com/tag/depth-first-search/) |
26 | 36 |
|
27 |
| -``` |
| 37 | +## 思路 |
28 | 38 |
|
29 |
| -## 思路 2 |
30 |
| -// 贴一些关键代码,说一些解题思路 |
31 |
| -```java |
32 |
| - |
33 |
| -``` |
| 39 | +题目给出一个二维数组来表示一张图像,数组里的值对应每个像素的值。指定一个目标像素,要求将所有和此像素连通(连通方向为上下左右四个方向)或间接连通的像素都更改为新的像素值`newColor`。 |
34 | 40 |
|
35 |
| -## 思路 3 |
36 |
| -// 贴一些关键代码,说一些解题思路 |
37 |
| -```kotlin |
| 41 | +简单的通过深度优先遍历将所有连通的像素改变即可。 |
38 | 42 |
|
| 43 | +```java |
| 44 | +class Solution { |
| 45 | + public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { |
| 46 | + int oldColor = image[sr][sc]; |
| 47 | + if (oldColor == newColor) { |
| 48 | + return image; |
| 49 | + } |
| 50 | + dfs(image, sr, sc, oldColor, newColor); |
| 51 | + return image; |
| 52 | + } |
| 53 | + |
| 54 | + private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) { |
| 55 | + if (image[sr][sc] == oldColor) { |
| 56 | + image[sr][sc] = newColor; |
| 57 | + if (sr > 0) dfs(image, sr - 1, sc, oldColor, newColor); |
| 58 | + if (sr + 1 < image.length) dfs(image, sr + 1, sc, oldColor, newColor); |
| 59 | + if (sc > 0) dfs(image, sr, sc - 1, oldColor, newColor); |
| 60 | + if (sc + 1 < image[sr].length) dfs(image, sr, sc + 1, oldColor, newColor); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
39 | 64 | ```
|
40 | 65 |
|
41 | 66 | ## 结语
|
42 |
| - |
| 67 | + |
43 | 68 | 如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
|
44 | 69 |
|
45 |
| -[title]: https://leetcode.com/problems/xxxx |
| 70 | +[title]: https://leetcode.com/problems/flood-fill/description/ |
46 | 71 | [ls]: https://github.com/RichCodersAndMe/LeetCode-Solution
|
0 commit comments