【leetcode】1034. 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 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 <= 50
1 <= grid[0].length <= 50
1 <= grid[i][j] <= 1000
0 <= r0 < grid.length
0 <= c0 < grid[0].length
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的更多相关文章
- 【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 ...
随机推荐
- Oracle Flashback Drop
Ensure that the prerequisites described in Prerequisites of Flashback Drop are met. The following li ...
- Mac开机自动运行shell脚本
1.首先写一个sh脚本,比如: cd ~/Documents mkdir haha 代码很简单,进入Documents文件夹,建立haha目录,保存为run.sh 2.修改run.sh权限 sudo ...
- XML读写工具类
摘要:①读取XML文件,生成pojo对象:②将对象信息保存到xml中. 步骤: ①新建一个普通的java类BasePage: package com.test.selenium.pages; impo ...
- Vue中解决路由切换,页面不更新的实用方法
前言:vue-router的切换不同于传统的页面的切换.路由之间的切换,其实就是组件之间的切换,不是真正的页面切换.这也会导致一个问题,就是引用相同组件的时候,会导致该组件无法更新,也就是我们口中的页 ...
- tensorflow学习笔记二:入门基础 好教程 可用
http://www.cnblogs.com/denny402/p/5852083.html tensorflow学习笔记二:入门基础 TensorFlow用张量这种数据结构来表示所有的数据.用一 ...
- 倾旋之slack主题协同
源:https://pocketcorp.slack.com/join/shared_invite/enQtNTk2MDYwNDA4NzU0LTg3ZGVlNDE5NWUzNjJhZTc1MDQ5MT ...
- 5款vue前端UI框架
Vue.js是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计. 实用的 Vue.js组件库可以帮助我们快速搭建页面,下面介绍小编认为比较受欢迎的五个vue ...
- 接口框架 python+unittest+request+HTMLTestRunner
request的各种方法主要用来处理客户端浏览器提交的请求中的各项参数和选项.如POST,GET最常用的两种请求 官方文档:http://docs.python-requests.org/en/mas ...
- 转载Django 500,404,400错误修改优化
转载:https://blog.csdn.net/qq_38038143/article/details/80105653 404错误:page not found视图 500错误:server er ...
- Spring IoC,IoC原理
一.IoC概念及原理 IOC的别名:依赖注入(DI) 2004年,Martin Fowler探讨了同一个问题,既然IOC是控制反转,那么到底是“哪些方面的控制被反转了呢?”,经过详细地分析和论证后,他 ...