当前位置:网站首页>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
边栏推荐
- Concept and use of CEPH distributed storage cluster pool resource pool
- Ajout, suppression et modification des données du tableau [MySQL] (DML)
- Sealem Finance打造Web3去中心化金融平台基础设施
- Part 7: Lesson 2 general skills of consultants - how to install and uninstall SAP ERP system client
- 数组 旋转数组
- 笔记(二)
- Record (III)
- How to realize the marketing purpose of small program integral mall
- Latex error: file ‘xxx. sty‘ not found
- String search in C
猜你喜欢

【phpstorm】 No data sources are configured to run this SQL and provide advanced c

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

2022-06-09 RK817 PMU 電池溫度檢測

oc swift 混编

【MySQL】錶數據的增删查改(DML)

Exec function of PHP
Super detailed tutorial for installing mysql8 in centos7 (no pit!)

Introduction to abbexa bacterial genome DNA Kit

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

If else is too easy to use? (understanding this article will further improve your logic)
随机推荐
数组 两数之和
Abbexa cell free DNA kit instructions
JVM runtime data area
Introduction to abbexa bacterial genome DNA Kit
数组 移动0
Different ways to create four indexes in MySQL
Several Apache related security vulnerability fixes
php的exec函数
Capacity expansion mechanism of ArrayList
(11) Tableview
Mysql中创建4种索引的不同方式
【phpstorm】 No data sources are configured to run this SQL and provide advanced c
2022-06-09 RK817 PMU 電池溫度檢測
oc swift 混编
Add, delete, query and modify MySQL table structure (DDL)
C language - quick sorting in sorting
Back to table query of MySQL? How to avoid it?
笔记(二)
【MySQL】表结构的增删查改操作(DDL)
How to view the occupied space of a table in MySQL database