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 <= 501 <= grid[0].length <= 501 <= grid[i][j] <= 10000 <= r0 < grid.length0 <= c0 < grid[0].length1 <= 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 ...
随机推荐
- 本地计算机上的SQL Server(MSSQLSERVER)服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
看下SQLExperss协议和 MySQLServer协议的TCP/IP端口,有可能是SQLEXPERSS将TCP端口占用了,将SQLEXPERSS的TCP/IP协议禁用,重启 SQLEXPRESS服 ...
- c# 在DataTable的第一列(指定列)的前面添加一列
c# 在DataTable的第一列(指定列)的前面添加一列 dt.Columns.Add("ID").SetOrdinal(0)
- Zuul的使用,路由访问映射规则
一.Zuul的介绍 Zuul包含了对请求的路由和过滤两个最主要的功能: 其中路由功能负责将外部请求转发到具体的微服务实力上,是实现外部访问统一入口基础而过滤器功能则负责对请求的处理过程进行干预,是实现 ...
- swiper轮播,添加鼠标移入事件停止轮播,移出重新开启轮播
已测过无问题.
- 【idea】【mysql】idea连接mysql
- SQL IN 一定走索引吗?
摘要 IN 一定走索引吗?那当然了,不走索引还能全部扫描吗?好像之前有看到过什么Exist,IN走不走索引的讨论.但是好像看的太久了,又忘记了.哈哈,如果你也忘记了MySQL中IN是如何查询的,就来复 ...
- php使用imagettftext()函数有干扰线但是没有文字的问题解决
public function code() { //主要参数 if($font_size == 0) $font_size = 20; if($img_width == 0) $img_width ...
- python 练习题:使用迭代查找一个list中最小和最大值,并返回一个tuple
# -*- coding: utf-8 -*- # 请使用迭代查找一个list中最小和最大值,并返回一个tuple from collections import Iterable def findM ...
- DSAPI CMD命令行进程代理
DSAPI.文件.CMD命令行进程代理,是用来和CMD.exe或指定exe进行输出重定向的简化使用工具,可隐藏或显式地启动一个cmd.exe,并将输出流.输入流和错误流重定向至代理,通过事件触发方式与 ...
- 如何禁用/关闭vs2017自带的Git工具的?
对于用习惯了独立Git工具和命令行的人来说,看到Visual Studio自带的Git工具后,很是别扭,到处充满了不习惯,而且是不是还会出现电脑卡顿的现象(可能是我自身电脑配置一般的问题). 如何关闭 ...