原题链接在这里: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. 1 <= grid.length <= 50
  2. 1 <= grid[0].length <= 50
  3. 1 <= grid[i][j] <= 1000
  4. 0 <= r0 < grid.length
  5. 0 <= c0 < grid[0].length
  6. 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;
}
}

类似Island Perimeter.

LeetCode 1034. Coloring A Border的更多相关文章

  1. 【leetcode】1034. Coloring A Border

    题目如下: Given a 2-dimensional grid of integers, each value in the grid represents the color of the gri ...

  2. [Swift]LeetCode1034.边框着色 | Coloring A Border

    Given a 2-dimensional grid of integers, each value in the grid represents the color of the grid squa ...

  3. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

  4. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  5. LeetCode Island Perimeter

    原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...

  6. Leetcode题解 - DFS部分题目代码+思路(756、1034、1110、491、721、988)

    756. 金字塔转换矩阵 """ 学到的新知识: from collections import defaultditc可以帮我们初始化字典,不至于取到某个不存在的值的时 ...

  7. [LeetCode] Design Snake Game 设计贪吃蛇游戏

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Qt Quick小项目 - 登陆界面

    开发环境: win8 + Qt5.11.2 说明: 用 QML 设计一个应用的登陆界面. 效果图: 新建一个 "Qt Quick Application - empty" 工程,分 ...

  2. contentType: 'application/json' C#后台怎么处理

    contentType: 'application/json' 的处理如下: $(function () { $.ajax({ 'url': "/Home/Send2SHengPi" ...

  3. Flink及Storm、Spark主流流框架比较

    转自:http://www.sohu.com/a/142553677_804130 引言 随着大数据时代的来临,大数据产品层出不穷.我们最近也对一款业内非常火的大数据产品 - Apache Flink ...

  4. spering getBean(),IOC

    IOC:前面都是对bean定义的处理,postProcess已经实例化了. 解析bean的时候,把需要依赖注入的字段和方法,在postProcessMergedBeanDefinition方法中加到A ...

  5. DirectX:Vector

    Tag DirectX下的博客主要用于记录DirectX的学习过程,主要参考<DirectX 12 3D 游戏实战开发>. Vector in DirectX Shader的编写离不开数学 ...

  6. java中的泛型【T】与通配符【?】概念入门

    使用泛型的目的是利用Java编译机制,在编译过程中帮我们检测代码中不规范的有可能导致程序错误的代码.例如,我们都知道List容器可以持有任何类型的数据,所以我们可以把String和Integer等类型 ...

  7. 数据库xp_cmdshell使用

    首先也开启组件. sp_configure reconfigure go sp_configure reconfigure go 删除本地文件,注意是删除数据库所在的服务器的本地文件. exec ma ...

  8. python 进程和线程-线程和线程变量ThreadLocal

    线程 线程是由若干个进程组成的,所以一个进程至少包含一个线程:并且线程是操作系统直接支持的执行单元.多任务可以由多进程完成,也可由一个进程的多个线程来完成 Python的线程是真正的Posix Thr ...

  9. vue知识点小结

    vue.js中==和===的区别 1.== 用于比较.判断两者相等,比较时可自动换数据类型 2.=== 用于(严格)比较.判断两者(严格)相等,不会进行自动转换,要求进行比较的操作数必须类型一致,不一 ...

  10. flask中的static_path和static_path_url和static_folder

    static_folder表示静态文件所在路径,默认为root_dir下的static文件夹 static_url_path的行为比较复杂 如果static_folder未被指定(也就是默认值stat ...