题目如下:

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

解题思路:题目本身不难,但是要理解清楚题目的意思。怎么判断一个方格是否是 The border of a connected component?有两个条件,一是这个方格处在grid的边界上,二是这个方格的上下左右四个方向的方格的颜色和自身不都一样。我的方法是分两步,第一步把和输入方格connect的所有方格都找出来,第二部就是依次判断这些connect的方格是否处于border,如果处于则修改颜色。

代码如下:

class Solution(object):
def colorBorder(self, grid, r0, c0, color):
"""
:type grid: List[List[int]]
:type r0: int
:type c0: int
:type color: int
:rtype: List[List[int]]
"""
visit = []
for i in grid:
visit.append([0] * len(i))
queue = [(r0,c0)]
visit[r0][c0] = 1
val = grid[r0][c0]
direction = [(1,0),(-1,0),(0,1),(0,-1)]
while len(queue) > 0:
x,y = queue.pop(0)
#grid[x][y] = color
for dx,dy in direction:
if x + dx >= 0 and x + dx < len(grid) and y +dy >=0 and y+dy < len(grid[0]) \
and visit[x+dx][y+dy] == 0 and val == grid[x+dx][y+dy]:
visit[x + dx][y + dy] = 1
queue.append((x+dx,y+dy)) for i in range(len(visit)):
for j in range(len(visit[i])):
if i == 1 and j == 1:
pass
if visit[i][j] == 0:
continue
elif i == 0 or i == len(visit) - 1 or j == 0 or j == len(visit[i]) - 1:
grid[i][j] = color
else:
for dx, dy in direction:
if i + dx >= 0 and i + dx < len(grid) and j + dy >= 0 and j + dy < len(grid[0]) \
and visit[i + dx][j + dy] == 0 :
grid[i][j] = color
break
#print visit
return grid

【leetcode】1034. Coloring A Border的更多相关文章

  1. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【arc073e】Ball Coloring(线段树,贪心)

    [arc073e]Ball Coloring(线段树,贪心) 题面 AtCoder 洛谷 题解 大型翻车现场,菊队完美压中男神的模拟题 首先钦定全局最小值为红色,剩下的袋子按照其中较大值排序. 枚举前 ...

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. Oracle OLAP

    w 国产商业智能 BI 这 20 年(1997-2017)| 天善智能  http://mp.weixin.qq.com/s/VJURX2qhmL0Uj1dctMyEig 1997-2001年,萌芽阶 ...

  2. Numpy和Pandas

    NumPy是高性能科学计算和数据分析的基础包.数据结构为ndarray,一般有三种方式来创建.ndarray是N-Dimensions-Array(N维数组)的简称,ndarray要求元素数据类型一致 ...

  3. Jmeter的中英文互换

    1.jmeter的中英文互换:为了更深入的了解Jmeter,一般使用英文版的jmeter. 旧版本下载的默认中文较多.新版本5.1的下载后默认英文比较的多. 方法一: 选项->选择一种语言,但是 ...

  4. Nacos 配置中心原理分析

    我们从原生SDK代码中入手,可以发现最核心的两行代码: ConfigService configService=); 首先我们先来看 NacosFactory.createConfigService ...

  5. MD5加密 和 自定义加密解密

    public class EncryptString { /// <summary> /// MD5加密 /// </summary> /// <param name=& ...

  6. 什么是 Java 对象深拷贝?面试必问!

    点击上方蓝色链接,关注并"设为星标" Java干货,每天及时推送 介绍 在Java语言里,当我们需要拷贝一个对象时,有两种类型的拷贝:浅拷贝与深拷贝. 浅拷贝只是拷贝了源对象的地址 ...

  7. V-Parenthesis 前缀+ZKW线段树或RMQ

    Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th questio ...

  8. 模板 - 可持久化无旋Treap

    空间消耗非常玄学,有多大开多大就完事了.其实是因为单次操作可能会有数次Merge和Split操作,按照下面的版本的话Merge和Split都进行复制,所以一次操作可能复制了4个版本. 四个函数式查询, ...

  9. P2586 [ZJOI2008]杀蚂蚁(模拟)

    P2586 [ZJOI2008]杀蚂蚁 大模拟. 什么都不想补了. 看变量名感性理解吧 #include<iostream> #include<cstdio> #include ...

  10. jQuery淡入淡出瀑布流效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...