ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

733. Flood Fill

2020-11-20 22:35:08  阅读:281  来源: 互联网

标签:newColor sr image Flood 733 sc starting pixel Fill


package LeetCode_733

import java.util.*

/**
 * 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.
 * */
class Solution {
    /*
    * solution: BFS, Time complexity:O(mn), Space complexity:O(mn)
    * */
    fun floodFill(image: Array<IntArray>, sr: Int, sc: Int, newColor: Int): Array<IntArray> {
        if (image == null || image.isEmpty()) {
            return image
        }
        val startingColor = image[sr][sc]
        if (startingColor == newColor) {
            return image
        }
        val queue = LinkedList<Pair<Int, Int>>()
        queue.offer(Pair(sr, sc))
        //set sr,sc as newColor
        image[sr][sc] = newColor
        val directions = intArrayOf(0, 1, 0, -1, 0)
        while (queue.isNotEmpty()) {
            val cur = queue.pop()
            //loop for 4 directions
            for (d in 0 until 4) {
                val newX = cur.first + directions[d]
                val newY = cur.second + directions[d + 1]
                //check boundary, and if new position color not same as starting color, skip it
                if (newX < 0 || newY < 0 || newX >= image.size || newY >= image[0].size || image[newX][newY] != startingColor) {
                    continue
                }
                //set current x,y as newColor
                image[newX][newY] = newColor
                queue.offer(Pair(newX, newY))
            }
        }
        return image
    }
}

 

标签:newColor,sr,image,Flood,733,sc,starting,pixel,Fill
来源: https://www.cnblogs.com/johnnyzhao/p/14013344.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有