leetcode130. Surrounded Regions

2023-10-21 13:20

本文主要是介绍leetcode130. Surrounded Regions,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题意:

130. Surrounded Regions

Medium

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 X
X O O X
X X O X
X O X X

After running your function, the board should be:

X X X X
X X X X
X X X X
X O X X

链接:

leetcode 130. Surrounded Regions

解题思路:

代码:

Java:

class Solution {public void solve(char[][] board) {if (board.length==0||board[0].length==0)    return;int n=board.length;int m=board[0].length;for(int i=0;i<n;++i){//左右两列if(board[i][0]=='O')    dfs(board,i,0);if(board[i][m-1]=='O')  dfs(board,i,m-1);}for(int i=0;i<m;++i){if(board[0][i]=='O')    dfs(board,0,i);if(board[n-1][i]=='O')  dfs(board,n-1,i);}for(int i=0;i<n;++i){for(int j=0;j<m;++j){if(board[i][j]=='O')    board[i][j]='X';else if(board[i][j]=='Y')   board[i][j]='O';}}}private void dfs(char[][] board,int i,int j){if(i<0||j<0||i>=board.length||j>=board[0].length||board[i][j]!='O') return;if(board[i][j]=='O')    board[i][j]='Y';int[] dx=new int[]{1,-1,0,0};int[] dy=new int[]{0,0,-1,1};for(int t=0;t<4;++t){int x=i+dx[t];int y=j+dy[t];dfs(board,x,y);}}
}

Python3:

class Solution:def solve(self, board: List[List[str]]) -> None:"""Do not return anything, modify board in-place instead."""if not board or not board[0]:   returnfor i in [0,len(board)-1]:for j in range(len(board[0])):self.dfs(board,i,j)for i in [0,len(board[0])-1]:for j in range(len(board)):self.dfs(board,j,i)for i in range(len(board)):for j in range(len(board[0])):if board[i][j]=='O':board[i][j]='X'elif board[i][j]=='Y':board[i][j]='O'def dfs(self,board,i,j):if 0 <= i < len(board) and 0 <= j < len(board[0]) and board[i][j] == 'O':board[i][j]='Y'dx=[-1,1,0,0]dy=[0,0,1,-1]for d in range(4):x=i+dx[d]y=j+dy[d]self.dfs(board,x,y)

 

 

 

这篇关于leetcode130. Surrounded Regions的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/254656

相关文章

【LeetCode】 Surrounded Regions (BFS DFS)

题目:Surrounded Regions 广搜和深搜都能解决,但是LeetCode上使用深搜时会栈溢出 DFS: <span style="font-size:18px;">/*LeetCode Surrounded Regions* 题目:给定一个字符数组,由'X'和'O'组成,找到所有被x包围的o并将其替换为x* 思路:只要替换被包围的o就行,如果有一个o是边界或者上下左右中有一个

Laravel 报错: Dotenv values containing spaces must be surrounded by quotes.

报错信息如下: 原因: .env文件配置中包含空格的配置信息,用双引号""引起来即可。 我是在配置项后面添加注释前面有空格,换行后显示正常。

LeetCode 题解(153): Surrounded Regions

题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X XX O O XX

LeetCode *** 130. Surrounded Regions

题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X XX O O X

文献阅读笔记《Spatial-temporal Forecasting for Regions without Observations》13页

目录 目录 目录 发行刊物 ABSTRACT 1 INTRODUCTION 2 RELATED WORK(相关工作 2.1 Spatial-temporal Forecasting(时空预测 2.2 Spatial-temporal Forecasting withIncomplete Data(不完全数据的时空预测 2.3 Graph Contrastive Learnin

Surrounded Regions --leetcode

题目: https://oj.leetcode.com/problems/surrounded-regions/ Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in

[LeetCode]130.Surrounded Regions

题目 Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’. A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region. For example, X X X X X O O X X X

《UE5_C++多人TPS完整教程》学习笔记11 ——《P12 Steam 地区(Steam Regions)》

本文为B站系列教学视频 《UE5_C++多人TPS完整教程》 —— P12 Steam 地区(Steam Regions)》 的学习笔记,该系列教学视频为 Udemy 课程 《Unreal Engine 5 C++ Multiplayer Shooter》 的中文字幕翻译版,UP主(也是译者)为 游戏引擎能吃么。 文章目录 P12 Steam 地区12.1 设置 Steam 地区1

[2018.04.17][水][日志][7][#188][USACO 3.1 Shaping Regions][漂浮大陆][背景-amp;amp;amp;gt;][表示为什么如此虚伪+纯模拟一只]

[背景]     这是我发的多少道模拟题了......     本道题表面和善,内在虚伪,因为,如果用纯模拟,你的程序将一塌糊涂.. [#188][USACO 3.1 Shaping Regions] 题目描述N个不同的颜色的不透明的长方形(1 <= N <= 1000)被放置在一张宽为A长为B的白纸上。这些长方形被放置时,保证了它们的边与白纸的边缘平行。所有的长方形都放置在白纸内,所以我

leetcode----130. Surrounded Regions

链接: https://leetcode.com/problems/surrounded-regions/ 大意: 给定一个二维字符数组board,board中每个字符都是'X'或者'O'。现在要求将所有被'X'包围'O'变为'X'。规定:在边界上的'O'默认不被'X'包围。例子: 思路: 图的DFS。 利用DFS,将board的各个边界为'O'以及与其连接的'O'全部变为'#'(