当前位置:网站首页>leetcode 130. Surrounded Regions 被围绕的区域(中等)
leetcode 130. Surrounded Regions 被围绕的区域(中等)
2022-06-10 21:17:00 【InfoQ】
一、题目大意

- m == board.length
- n == board[i].length
- 1 <= m, n <= 200
- board[i][j] 为 'X' 或 'O'
二、解题思路
三、解题方法
3.1 Java实现
public class Solution {
public void solve(char[][] board) {
this.m = board.length;
if (this.m == 0) {
return;
}
this.board = board;
this.n = board[0].length;
for (int y = 0; y < m; y++) {
dfs(0, y);
dfs(n - 1, y);
}
for (int x = 0; x < n; x++) {
dfs(x, 0);
dfs(x, m - 1);
}
Map<Character, Character> v = new HashMap<>();
v.put('G', 'O');
v.put('O', 'X');
v.put('X', 'X');
for (int y = 0; y < m; y++) {
for (int x = 0; x < n; x++) {
switch (board[y][x]) {
case 'G':
board[y][x] = 'O';
break;
case 'O':
board[y][x] = 'X';
break;
case 'X':
board[y][x] = 'X';
}
}
}
}
private char[][] board;
private int m;
private int n;
private void dfs(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= m || board[y][x] != 'O') {
return;
}
board[y][x] = 'G';
dfs(x - 1, y);
dfs(x + 1, y);
dfs(x, y - 1);
dfs(x, y + 1);
}
}
四、总结小记
- 2022/6/10 联通分量问题用DFS
边栏推荐
- Exec function of PHP
- Apple zoom! It's done so well
- C语言判断文件或文件夹是否存在
- Detailed explanation of MySQL range query optimization scenario instances
- [problem] solve the problem of websocket string length limitation. The single packet is too large
- Pytorch 安装超简单
- 数组 从指定长度位旋转数组
- Has the samesite cookie problem occurred when using identityserver?
- What are MySQL clustered indexes and nonclustered indexes?
- 【phpstorm】 No data sources are configured to run this SQL and provide advanced c
猜你喜欢

Sealem Finance打造Web3去中心化金融平台基础设施

【MySQL】錶數據的增删查改(DML)
![[MySQL] Table constraints](/img/13/b97679cd4ca36d4b8e19acf23df404.png)
[MySQL] Table constraints

【Debug】could not find ref wiht poc XXX解决

【MySQL】表结构的增删查改操作(DDL)

Icml2022 | sharp maml: model independent meta learning for sharpness perception

AI blessing real-time interaction | analysis of zegoavatar facial expression following technology

leetcode:333. Maximum BST subtree

Abbexa cdan1 siRNA instruction manual

【Microsoft Azure 的1024种玩法】七十五.云端数据库迁移之快速将阿里云RDS SQL Server无缝迁移到Azure SQL Databas
随机推荐
Array remove duplicates from an array
C language to judge whether a file or folder exists
Mysql的回表查询?如何避免?
Array intersection of two arrays II
GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.
What are MySQL clustered indexes and nonclustered indexes?
Bitwise and shift operators
leetcode:333. 最大 BST 子树
C语言-排序中的快速排序(简称快排)
[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas
系统重装以及查询系统性能
Modify frontsortinglayer variable of spritemask
Back to table query of MySQL? How to avoid it?
If else is too easy to use? (understanding this article will further improve your logic)
Business based precipitation component = & gt; manage-table
Array union set
Only this is the most true reason why leaders promote you. The rest is nonsense!
变量(自动变量、静态变量、寄存器变量、外部变量)与C的内存分配malloc/free、calloc/recalloc
Abbexa 1,3-dipalmitonin CLIA kit solution
Variables (automatic variables, static variables, register variables, external variables) and memory allocation of C malloc/free, calloc/realloc