LeetCode 733. Flood Fill

原题链接在这里:https://leetcode.com/problems/flood-fill/

题目:

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

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.

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.

At the end, return the modified image.

Example 1:

Input: 
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation: 
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected 
by a path of the same color as the starting pixel are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected
to the starting pixel.

Note:

  • The length of image and image[0] will be in the range [1, 50].
  • The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
  • The value of each color in image[i][j] and newColor will be an integer in [0, 65535].

题解:

Could fill the image with BFS.

Time Complexity: O(m * n). m = image.length. n = image[0].length.

Space: O(m * n).

AC Java:

 1 class Solution {
 2     int [][] dirs = new int[][]{{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
 3     
 4     public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
 5         if(image == null || sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] == newColor){
 6             return image;
 7         }
 8         
 9         int m = image.length;
10         int n = image[0].length;
11         int oriCol = image[sr][sc];
12         LinkedList<int []> que = new LinkedList<>();
13         image[sr][sc] = newColor;
14         que.add(new int[] {sr, sc});
15         while(!que.isEmpty()){
16             int [] cur = que.poll();
17             for(int [] dir : dirs){
18                 int x = cur[0] + dir[0];
19                 int y = cur[1] + dir[1];
20                 if(x < 0 || x >= m || y < 0 || y >=n || image[x][y] != oriCol){
21                     continue;
22                 }
23                 
24                 image[x][y] = newColor;
25                 que.add(new int[]{x, y});
26             }
27         }
28         
29         return image;
30     }
31 }

Could use DFS as well.

Time Complexity: O(m * n).

Space: O(m * n).

AC Java:

 1 class Solution {
 2     public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
 3         if(image == null || sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] == newColor){
 4             return image;
 5         }
 6         
 7         dfs(image, sr, sc, image[sr][sc], newColor);
 8         return image;
 9     }
10     
11     private void dfs(int [][] image, int i, int j, int oriColor, int newColor){
12         if(i < 0 || i >= image.length || j < 0 || j>= image[0].length || image[i][j] != oriColor){
13             return;
14         }
15         
16         image[i][j] = newColor;
17         dfs(image, i + 1, j, oriColor, newColor);
18         dfs(image, i - 1, j, oriColor, newColor);
19         dfs(image, i, j + 1, oriColor, newColor);
20         dfs(image, i, j - 1, oriColor, newColor);
21     }
22 }

类似Max Area of Island.

posted @ 2019-12-18 10:59  Dylan_Java_NYC  阅读(217)  评论(0编辑  收藏  举报