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字符串各种颜色输出
\033[1;31;40m # 1是显示方式(可选),31是字体颜色,40m 是字体背景颜色: \033[0m # 恢复终端默认颜色,即取消颜色设置: #!/usr/bi ...
- Qt Quick 常用元素:ComboBox(下拉列表) 与 ProgressBar(进度条)
一.ComboBox ComboBox,即下拉列表框,由一个列表框和一个标签控件(或编辑控件)组成.ComboBox 的下拉列表是使用 Menu 实现的,列表内的每个条目对应一个 Menultem. ...
- bash: telnet: command not found (Linux安装telnet)
问题描述: centos 系统没有 telnet 命令 bash: telnet: command not found 1.安装telnet服务 (3个) yum install xinetd tel ...
- <T>泛型,广泛的类型
其实早在1999年的JSR 14规范中就提到了泛型概念,知道jdk5泛型的使用才正式发布,在jdk7后,又对泛型做了优化,泛型的推断. 泛型类 public class Pair<T> { ...
- HTML+css基础 表格标签table Table标签属性 td标签属性
表格标签table: 他是由行与列构成,最小单位是单元格. 行标签 <tr></tr> 单元格标签<td></td> Table标签属性: Bor ...
- spring boot中的日志入门
日志通常不会在需求阶段作为一个功能单独提出来,也不会在产品方案中看到它的细节.但是,这丝毫不影响它在任何一个系统中的重要地位. 报警系统与日志系统的关系 为了保证服务的高可用,发现问题一定要及时,定位 ...
- MySQL:实用 SQL 语句集合
写在前面的话 本文主要用于记录工作中不经常使用但是偶尔用到又非常有用的 SQL 语句,持续不断不定期更新. 数据库大小统计 1. 查看 MySQL 某个库的所有表大小,记录数,占用空间等. ,), ' ...
- hadoop格式化:java.io.IOException: Incompatible clusterIDs in /home/lxh/hadoop/hdfs/data: namenode clusterID
1 概述 解决hadoop启动hdfs时,datanode无法启动的问题.错误为: java.io.IOException: Incompatible clusterIDs in /home/lxh ...
- 制作一个SSRS的ORACLE数据库报表,使用了时间类型的参数。
需求:我们这个报表是以月为单位,呈现的数据为查询为当前月的第一天到最后一天.条件类似于:time_day > 20140601 and time_day < 20140630 因为是让用 ...
- 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping。
问题: 该错误是因为应用程序需要jQuery,但是当前项目中并没有jQuery,或者存在jQuery但是程序不知道jQuery的存放路径. 解决方案: 一.下载jQuery,引入必要的jquery-X ...