LeetCode 1034. Coloring A Border
原题链接在这里:https://leetcode.com/problems/coloring-a-border/
题目:
Given a 2-dimensional grid
of integers, each value in the grid represents the color of the grid square at that location.
Two squares belong to the same connected component if and only if they have the same color and are next to each other in any of the 4 directions.
The border of a connected component is all the squares in the connected component that are either 4-directionally adjacent to a square not in the component, or on the boundary of the grid (the first or last row or column).
Given a square at location (r0, c0)
in the grid and a color
, color the border of the connected component of that square with the given color
, and return the final grid
.
Example 1:
Input: grid = [[1,1],[1,2]], r0 = 0, c0 = 0, color = 3
Output: [[3, 3], [3, 2]]
Example 2:
Input: grid = [[1,2,2],[2,3,2]], r0 = 0, c0 = 1, color = 3
Output: [[1, 3, 3], [2, 3, 3]]
Example 3:
Input: grid = [[1,1,1],[1,1,1],[1,1,1]], r0 = 1, c0 = 1, color = 2
Output: [[2, 2, 2], [2, 1, 2], [2, 2, 2]]
Note:
1 <= grid.length <= 50
1 <= grid[0].length <= 50
1 <= grid[i][j] <= 1000
0 <= r0 < grid.length
0 <= c0 < grid[0].length
1 <= color <= 1000
题解:
Could use BFS to find the border.
For current cell, its 4 neighbors, if any of them is out of boundary, or different color than grid[r0][c0], this is the border. Mark it.
Here can't change to new color yet, since later neighbor's neighbor may check this value, it needs to remain to its original color when doing BFS.
After BFS, iterate the grid again and change the border color.
Time Complexity: O(m * n). m = gird.length. n = grid[0].length.
Space: O(m * n).
AC Java:
class Solution {
public int[][] colorBorder(int[][] grid, int r0, int c0, int color) {
if(grid == null || r0 < 0 || r0 >= grid.length || c0 < 0 || c0 >= grid[0].length || grid[r0][c0] == color){
return grid;
} int m = grid.length;
int n = grid[0].length;
int oriColor = grid[r0][c0];
LinkedList<int []> que = new LinkedList<>();
boolean [][] visited = new boolean[m][n];
boolean [][] border = new boolean[m][n];
que.add(new int[]{r0, c0});
visited[r0][c0] = true;
int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; while(!que.isEmpty()){
int [] cur = que.poll();
boolean isBorder = false; for(int [] dir : dirs){
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != oriColor){
isBorder = true;
continue;
} if(visited[x][y]){
continue;
} que.add(new int[]{x, y});
visited[x][y] = true;
} if(isBorder){
border[cur[0]][cur[1]] = isBorder;
}
} for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(border[i][j]){
grid[i][j] = color;
}
}
} return grid;
}
}
LeetCode 1034. Coloring A Border的更多相关文章
- 【leetcode】1034. Coloring A Border
题目如下: Given a 2-dimensional grid of integers, each value in the grid represents the color of the gri ...
- [Swift]LeetCode1034.边框着色 | Coloring A Border
Given a 2-dimensional grid of integers, each value in the grid represents the color of the grid squa ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode Island Perimeter
原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...
- Leetcode题解 - DFS部分题目代码+思路(756、1034、1110、491、721、988)
756. 金字塔转换矩阵 """ 学到的新知识: from collections import defaultditc可以帮我们初始化字典,不至于取到某个不存在的值的时 ...
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
随机推荐
- 一张图入门Python【中文版】
好久没写了,就拿这张图作为开篇吧,重新梳理自己学习的东西,最近两年人工智能炒红了python,devops的提出也把开发.运维整合到了一起,作为一个运维工程师,随着企业自动化运维的提出,光会shell ...
- Debug 路漫漫-11:Python: TypeError: 'generator' object is not subscriptable
调试程序,出现以下错误: Python: TypeError: 'generator' object is not subscriptable “在Python中,这种一边循环一边计算的机制,称为生成 ...
- 《Interest Rate Risk Modeling》阅读笔记——第四章:M-absolute 和 M-square 风险度量
目录 第四章:M-absolute 和 M-square 风险度量 思维导图 两个重要不等式的推导 关于 \(M^A\) 的不等式 关于 \(M^2\) 的不等式 凸性效应(CE)和风险效应(RE)的 ...
- 洛谷疯狂coding~
1.关于数学建模思想在coding之中的应用. 将马路作为一条数轴,每棵树的位置作为数轴上的坐标点,再将坐标点与数组的下标联系到一起,完成建模. 2.本题坑点在于对“其中有多少个数,恰好等于集合中另外 ...
- MonkeyDev安装--逆向开发
MonkeyDev是原有iOS OpenDev的升级,非越狱插件的开发集成神器! 可以使用Xcode开发CaptainHook Tweak.Logos Tweak 和 Command-line Too ...
- 微信小程序报thirdScriptError Cannot read property 'setData' of undefined
用onLoad: function (options) {} 加载页面缓存的数据的时候报错 修改为
- thinkphp3.2.3使用formdata的多文件上传
使用formdata的多文件上传 废话少说 直接上代码 1 JS部分 //选择文件后的处理 function handleFileSelect() { var exerciseid=$(" ...
- 关于wordpress4.8中的Twenty Seventeen主题的主题选项增加章节的实现
我这里的wordpress版本是4.8 默认的主题是 Twenty Seventeen 我想实现的事 主题选项的首页 多增加2个章节 默认是只有4个章节 我想在增加2个 到6个 看下实现后的效果 ...
- 2019-11-29-WPF-笔刷绑定不上可能的原因
原文:2019-11-29-WPF-笔刷绑定不上可能的原因 title author date CreateTime categories WPF 笔刷绑定不上可能的原因 lindexi 2019-1 ...
- net dll 重新签名
已经有强签名的dll或exe程序无法引用无签名的dll,这时候就需要对dll进行签名,签名的步骤如下: 为没有源码的DLL文件添加强名称 如果项目中引用了其他没有源码的dll文件,并且此dll文件是没 ...