【leetcode】1034. Coloring A Border
题目如下:
Given a 2-dimensional
gridof 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 acolor, color the border of the connected component of that square with the givencolor, and return the finalgrid.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
解题思路:题目本身不难,但是要理解清楚题目的意思。怎么判断一个方格是否是 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的更多相关文章
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【arc073e】Ball Coloring(线段树,贪心)
[arc073e]Ball Coloring(线段树,贪心) 题面 AtCoder 洛谷 题解 大型翻车现场,菊队完美压中男神的模拟题 首先钦定全局最小值为红色,剩下的袋子按照其中较大值排序. 枚举前 ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- qbzt day1 上午
内容提要 模拟,贪心 在讲这些东西之前,我们先来了解一个东西:high level 这个东西大体上就是你做题之前要先想清楚自己要写什么,怎么写,然后再写,不要有一点写一点 1.模拟 模拟算法算是很水的 ...
- 虚拟主机支持apk
扩展名中填写.apk MIME类型中填写apk的MIME类型 application/vnd.android.package-archive
- Django中间件添加白名单
一定记得配置 补充一点中间件是工作流程 中间件的详细流程 补充一点需求:在不用中间件的情况和下用装饰器做登陆的阻挡 在django中有自带的 登陆闭包函数只需要引出来就可以直接用了下面是步骤 在se ...
- VMware 虚拟化编程(13) — VMware 虚拟机的备份方案设计
目录 目录 前文列表 备份思路 备份算法 备份细节 连接到 vCenter 还是 ESXi 如何选择快照类型 是否开启 CBT 如何获取备份数据 如何提高备份数据的传输率 备份厚置备磁盘和精简置备磁盘 ...
- 测开之路九十八:js变量和语句
这里为了方便调试,在jsbin网站上面编写js脚本:https://jsbin.com/?js,console 可以点击增加/减少对应展示分页,Console为控制台部分,Output为页面部分 变量 ...
- 用 Redis 实现 PHP 的简单消息队列
参考:PHP高级编程之消息队列 消息队列就是在消息的传输过程中,可以保存消息的容器. 常见用途: 存储转发:异步处理耗时的任务 分布式事务:多个消费者消费同一个消息队列 应对高并发:通过消息队列保存任 ...
- linux优化
优化linux启动项 1. 使用ntsysv工具讲不需要的服务关闭 2. 默认启动服务可以只保留必要的服务 3. free -m 以m为单位 4. 删除不必要的用户: 5. cp /etc/passw ...
- JAVA总结--分布式锁
1.概念 分布式锁出现的原因:单体应用单机部署环境下,为了解决多线程并发问题,我们会使用ReentrantLcok或synchronized来解决互斥问题:但业务的需求,单机部署演变成分布式系统后,在 ...
- FZUOJ-2273 Triangles
Problem 2273 Triangles Accept: 109 Submit: 360 Time Limit: 1000 mSec Memory Limit : 262144 KB ...
- 大神级回答exists与in的区别
google搜了一下,很多帖子,而且出发点不同,各有各的道理,但是有一个帖子讲的特别好: http://zhidao.baidu.com/question/134174568.html 忍不住在百度上 ...