leetcode1034
class Solution:
def __init__(self):
self.V = list() def bfs(self,grid,color,rows,coloums,r,c,ocolor):
cur = [r,c]
if cur[0]-1 >=0 and self.V[cur[0]-1][cur[1]]==0 and grid[cur[0]-1][cur[1]]==ocolor:
grid[cur[0]-1][cur[1]] = color
self.V[cur[0]-1][cur[1]] = 1
self.bfs(grid,color,rows,coloums,cur[0]-1,cur[1],ocolor)
if cur[0]+1 <rows and self.V[cur[0]+1][cur[1]]==0 and grid[cur[0]+1][cur[1]] == ocolor:
grid[cur[0]+1][cur[1]] = color
self.V[cur[0]+1][cur[1]] = 1
self.bfs(grid,color,rows,coloums,cur[0]+1,cur[1],ocolor)
if cur[1]-1 >=0 and self.V[cur[0]][cur[1]-1]==0 and grid[cur[0]][cur[1]-1] == ocolor:
grid[cur[0]][cur[1]-1] = color
self.V[cur[0]][cur[1]-1] = 1
self.bfs(grid,color,rows,coloums,cur[0],cur[1]-1,ocolor)
if cur[1]+1 <coloums and self.V[cur[0]][cur[1]+1]==0 and grid[cur[0]][cur[1]+1]==ocolor:
grid[cur[0]][cur[1]+1] = color
self.V[cur[0]][cur[1]+1] = 1
self.bfs(grid,color,rows,coloums,cur[0],cur[1]+1,ocolor) def colorBorder(self, grid: 'List[List[int]]', r0: int, c0: int, color: int) -> 'List[List[int]]':
if grid[r0][c0] != color:
ocolor = grid[r0][c0]
rows = len(grid)
coloums = len(grid[0])
self.V = [[0 for c in range(coloums)] for r in range(rows)]
self.V[r0][c0] = 1
grid[r0][c0] = color
self.bfs(grid,color,rows,coloums,r0,c0,ocolor)
print(self.V)
for i in range(rows):
for j in range(coloums):
if i>0 and i<rows-1 and j>0 and j<coloums-1:
if self.V[i][j] == 1 and self.V[i-1][j]==1 and self.V[i+1][j]==1 and self.V[i][j-1]==1 and self.V[i][j+1]==1:
grid[i][j] = ocolor
return grid
这题是什么鬼玩意儿?是我打开方式不对么。。。
leetcode1034的更多相关文章
- [Swift]LeetCode1034.边框着色 | Coloring A Border
Given a 2-dimensional grid of integers, each value in the grid represents the color of the grid squa ...
随机推荐
- java面试总躲不过的并发(二):volatile原理 + happens-before原则
一.happens-before原则 同一个线程中的,前面的操作 happens-before 后续的操作.(即单线程内按代码顺序执行.但是,在不影响在单线程环境执行结果的前提下,编译器和处理器可以进 ...
- cordova闪屏插件插件使用:cordova-plugin-splashscreen
欢迎页本地插件,默认建议包含.启动本地应用时显示指定的图片(启动页) 1. 添加插件:cordova plugin add cordova-plugin-splashscreen 2. 调用方法:
- LeetCode - Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- Linux----------mysql进阶
目录 一.破解密码以及无密码登录 1.1 破解密码 1.2 无密码登录 1.3 定义不同的客户端 1.4 家目录下 二.视图 三.函数 3.1 系统函数 3.2 自定义函数 3.3 自定义函数中定义局 ...
- css多行省略
单行省略就不用说了,用css实现非常简单,兼容性还非常好.但是多行省略一直都是前端的痛点,在css3之前,可以用js去算两行能放多少个字,把多余的字用 ... 代替,且不说好不好,万一哪天PM说要改成 ...
- sharding sphere 分表分库 读写分离
sharding jdbc: sharding sphere 的 一部分,可以做到 分表分库,读写分离. 和 mycat 不同的 是 sharding jdbc 是 一个 jdbc 驱动 在 驱动这个 ...
- Dynamics 365—脚本
Xrm.Page.getAttribute() 转控件:controls.get(0) 取赋值:getValue(),setValue() 是否改动:getIsDirty() 表单载入时的值:getI ...
- python爬虫---selenium库的用法
python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...
- SSHD启动失败,错误码255
查看/etc/ssh/sshd_config 发现,Listen Address并不是我想要的ip,将其注释掉 sshd restart,结果返回 Permission denied (publick ...
- EntityFramework Inner Exception Catch
在保存时加入这一段,就可以查看error具体是哪里出错了.正式发布需要删除这段,try catch毕竟会影响性能 try { entity.SaveChanges(); } catch (DbEnti ...