当前位置:网站首页>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
边栏推荐
- Differences between disk serial number, disk ID and volume serial number
- JVM运行时数据区
- oc swift 混编
- php的exec函数
- Abbexa low sample size chicken lysozyme C (Lyz) ELISA Kit
- Detailed explanation of Lora module wireless transceiver communication technology
- Notes (V) - JVM
- torch_ geometric
- Are you still writing the TS type code
- Cordova plugin /jpush phonegap Aurora push_ Local push_ Message push
猜你喜欢

datagrip 报错 “The specified database user/password combination is rejected...”的解决方法

Mysql 什么是聚集索引和非聚集索引?

【Xpath】使用following-sibling获取后面的同级节点

What are MySQL clustered indexes and nonclustered indexes?

如何激发文化创新的活力和驱动力

Differences between disk serial number, disk ID and volume serial number

Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation

Abbexa 8-OHdG CLIA kit solution

SQL第四练:字符串处理函数

Principle of gravure overprint and factors affecting overprint
随机推荐
PHP pseudo protocol implementation command execution details
PMP candidates, there are these places for PMP examination in Shenzhen in June 2022
ThinkPHP v6.0. X deserialization vulnerability recurrence
【MySQL】表结构的增删查改操作(DDL)
torch_ geometric
Array union set
Solution to "XXX has broken path" error in idea
Has the samesite cookie problem occurred when using identityserver?
Can I make up the exam if I fail the soft exam? Here comes the answer
【Debug】could not find ref wiht poc XXX解决
Abbexa acrylamide peg NHS instructions
Differences between disk serial number, disk ID and volume serial number
What are MySQL clustered indexes and nonclustered indexes?
C语言判断文件或文件夹是否存在
Array plus one
C中字符串查找
数组 加一
Record (III)
Pytorch installation is super simple
修改SpriteMask 的 frontSortingLayer 变量