zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    OpenStack Trail 部署文档(二)基础服务部署
    OpenStack Trail 部署文档(一)环境规划
    OpenStack Trail 部署文档
    配置kubectl在Mac(本地)远程连接Kubernetes集群
    elasticsearch*3 + Es-Head + kibana Docker集群
    Flex 布局教程:语法篇
    PHP 数组 array_merge 和 数组 + 加号操作的区别
    Redis分布式锁
    Mysql中Exists和In的使用
    让PHP7达到最高性能的几个Tips
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12058727.html
Copyright © 2011-2022 走看看