博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode130. Surrounded Regions (思路及python解法)
阅读量:2242 次
发布时间:2019-05-09

本文共 867 字,大约阅读时间需要 2 分钟。

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region.

Example:

X X X XX O O XX X O XX O X X

After running your function, the board should be:

X X X XX X X XX X X XX O X X

和连通区域比较类似,不过是和边界为“O”的连通区域。

这道题我用了BFS的算法,主要分三步。

1、把边界为“O”的点放到队列中

2、把队列的连通区域标记出来

3、把没有标记的都设为“X”,把所有标记的都设为“O”。

class Solution:    def solve(self, board: List[List[str]]) -> None:        """        Do not return anything, modify board in-place instead.        """        if not board:return board        r, c = len(board), len(board[0])        bfs=[]        for i in range(r):            for j in range(c):                if (i in [0,r-1] or j in [0,c-1]) and board[i][j]=='O':                    bfs.append([i,j])        while bfs:            i, j = bfs.pop(0)            if 0<=i

 

转载地址:http://rdrbb.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
【JMeter】1.9上考试jmeter测试调试
查看>>
【虫师】【selenium】参数化
查看>>
【Python练习】文件引用用户名密码登录系统
查看>>
学习网站汇总
查看>>
【Loadrunner】性能测试报告实战
查看>>
【自动化测试】自动化测试需要了解的的一些事情。
查看>>
【selenium】selenium ide的安装过程
查看>>
【手机自动化测试】monkey测试
查看>>