Skip to content

Commit d9ed47c

Browse files
committed
+ problem 130
1 parent 0a50a97 commit d9ed47c

File tree

5 files changed

+242
-0
lines changed

5 files changed

+242
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 130. Surrounded Regions
2+
You are given an `m x n` matrix `board` containing **letters** `'X'` and `'O'`, **capture regions** that are **surrounded**:
3+
* **Connect:** A cell is connected to adjacent cells horizontally or vertically.
4+
* **Region:** To form a region connect every `'O'` cell.
5+
* **Surround:** The region is surrounded with `'X'` cells if you can **connect the region** with `'X'` cells and none of the region cells are on the edge of the `board`.
6+
7+
To capture a **surrounded region**, replace all `'O'`s with `'X'`s **in-place** within the original board. You do not need to return anything.
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
12+
<strong>Output:</strong> [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
13+
<strong>Explanation:</strong>
14+
<img src=https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg>
15+
In the above diagram, the bottom region is not captured because it is on the edge of the board and cannot be surrounded.
16+
</pre>
17+
18+
#### Example 2:
19+
<pre>
20+
<strong>Input:</strong> board = [["X"]]
21+
<strong>Output:</strong> [["X"]]
22+
</pre>
23+
24+
#### Constraints:
25+
* `m == board.length`
26+
* `n == board[i].length`
27+
* `1 <= m, n <= 200`
28+
* `board[i][j]` is `'X'` or `'O'`.
29+
30+
## Solutions (Rust)
31+
32+
### 1. Solution
33+
```Rust
34+
impl Solution {
35+
pub fn solve(board: &mut Vec<Vec<char>>) {
36+
let (m, n) = (board.len(), board[0].len());
37+
let mut stack = vec![];
38+
39+
for i in 0..m {
40+
if board[i][0] == 'O' {
41+
board[i][0] = '0';
42+
stack.push((i, 0));
43+
}
44+
if board[i][n - 1] == 'O' {
45+
board[i][n - 1] = '0';
46+
stack.push((i, n - 1));
47+
}
48+
}
49+
for j in 0..n {
50+
if board[0][j] == 'O' {
51+
board[0][j] = '0';
52+
stack.push((0, j));
53+
}
54+
if board[m - 1][j] == 'O' {
55+
board[m - 1][j] = '0';
56+
stack.push((m - 1, j));
57+
}
58+
}
59+
60+
while let Some((i, j)) = stack.pop() {
61+
if i > 0 && board[i - 1][j] == 'O' {
62+
board[i - 1][j] = '0';
63+
stack.push((i - 1, j));
64+
}
65+
if i < m - 1 && board[i + 1][j] == 'O' {
66+
board[i + 1][j] = '0';
67+
stack.push((i + 1, j));
68+
}
69+
if j > 0 && board[i][j - 1] == 'O' {
70+
board[i][j - 1] = '0';
71+
stack.push((i, j - 1));
72+
}
73+
if j < n - 1 && board[i][j + 1] == 'O' {
74+
board[i][j + 1] = '0';
75+
stack.push((i, j + 1));
76+
}
77+
}
78+
79+
for i in 0..m {
80+
for j in 0..n {
81+
if board[i][j] == '0' {
82+
board[i][j] = 'O';
83+
} else if board[i][j] == 'O' {
84+
board[i][j] = 'X';
85+
}
86+
}
87+
}
88+
}
89+
}
90+
```
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 130. 被围绕的区域
2+
给你一个 `m x n` 的矩阵 `board` ,由若干字符 `'X'``'O'` 组成,**捕获** 所有 **被围绕的区域**
3+
* **连接:**一个单元格与水平或垂直方向上相邻的单元格连接。
4+
* **区域:**连接所有 `'O'` 的单元格来形成一个区域。
5+
* **围绕:**如果您可以用 `'X'` 单元格 **连接这个区域**,并且区域中没有任何单元格位于 `board` 边缘,则该区域被 `'X'` 单元格围绕。
6+
7+
通过 **原地** 将输入矩阵中的所有 `'O'` 替换为 `'X'`**捕获被围绕的区域**。你不需要返回任何值。
8+
9+
#### 示例 1:
10+
<pre>
11+
<strong>输入:</strong> board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
12+
<strong>输出:</strong> [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
13+
<strong>解释:</strong>
14+
<img src=https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg>
15+
在上图中,底部的区域没有被捕获,因为它在 board 的边缘并且不能被围绕。
16+
</pre>
17+
18+
#### 示例 2:
19+
<pre>
20+
<strong>输入:</strong> board = [["X"]]
21+
<strong>输出:</strong> [["X"]]
22+
</pre>
23+
24+
#### 提示:
25+
* `m == board.length`
26+
* `n == board[i].length`
27+
* `1 <= m, n <= 200`
28+
* `board[i][j]``'X'``'O'`
29+
30+
## 题解 (Rust)
31+
32+
### 1. 题解
33+
```Rust
34+
impl Solution {
35+
pub fn solve(board: &mut Vec<Vec<char>>) {
36+
let (m, n) = (board.len(), board[0].len());
37+
let mut stack = vec![];
38+
39+
for i in 0..m {
40+
if board[i][0] == 'O' {
41+
board[i][0] = '0';
42+
stack.push((i, 0));
43+
}
44+
if board[i][n - 1] == 'O' {
45+
board[i][n - 1] = '0';
46+
stack.push((i, n - 1));
47+
}
48+
}
49+
for j in 0..n {
50+
if board[0][j] == 'O' {
51+
board[0][j] = '0';
52+
stack.push((0, j));
53+
}
54+
if board[m - 1][j] == 'O' {
55+
board[m - 1][j] = '0';
56+
stack.push((m - 1, j));
57+
}
58+
}
59+
60+
while let Some((i, j)) = stack.pop() {
61+
if i > 0 && board[i - 1][j] == 'O' {
62+
board[i - 1][j] = '0';
63+
stack.push((i - 1, j));
64+
}
65+
if i < m - 1 && board[i + 1][j] == 'O' {
66+
board[i + 1][j] = '0';
67+
stack.push((i + 1, j));
68+
}
69+
if j > 0 && board[i][j - 1] == 'O' {
70+
board[i][j - 1] = '0';
71+
stack.push((i, j - 1));
72+
}
73+
if j < n - 1 && board[i][j + 1] == 'O' {
74+
board[i][j + 1] = '0';
75+
stack.push((i, j + 1));
76+
}
77+
}
78+
79+
for i in 0..m {
80+
for j in 0..n {
81+
if board[i][j] == '0' {
82+
board[i][j] = 'O';
83+
} else if board[i][j] == 'O' {
84+
board[i][j] = 'X';
85+
}
86+
}
87+
}
88+
}
89+
}
90+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
impl Solution {
2+
pub fn solve(board: &mut Vec<Vec<char>>) {
3+
let (m, n) = (board.len(), board[0].len());
4+
let mut stack = vec![];
5+
6+
for i in 0..m {
7+
if board[i][0] == 'O' {
8+
board[i][0] = '0';
9+
stack.push((i, 0));
10+
}
11+
if board[i][n - 1] == 'O' {
12+
board[i][n - 1] = '0';
13+
stack.push((i, n - 1));
14+
}
15+
}
16+
for j in 0..n {
17+
if board[0][j] == 'O' {
18+
board[0][j] = '0';
19+
stack.push((0, j));
20+
}
21+
if board[m - 1][j] == 'O' {
22+
board[m - 1][j] = '0';
23+
stack.push((m - 1, j));
24+
}
25+
}
26+
27+
while let Some((i, j)) = stack.pop() {
28+
if i > 0 && board[i - 1][j] == 'O' {
29+
board[i - 1][j] = '0';
30+
stack.push((i - 1, j));
31+
}
32+
if i < m - 1 && board[i + 1][j] == 'O' {
33+
board[i + 1][j] = '0';
34+
stack.push((i + 1, j));
35+
}
36+
if j > 0 && board[i][j - 1] == 'O' {
37+
board[i][j - 1] = '0';
38+
stack.push((i, j - 1));
39+
}
40+
if j < n - 1 && board[i][j + 1] == 'O' {
41+
board[i][j + 1] = '0';
42+
stack.push((i, j + 1));
43+
}
44+
}
45+
46+
for i in 0..m {
47+
for j in 0..n {
48+
if board[i][j] == '0' {
49+
board[i][j] = 'O';
50+
} else if board[i][j] == 'O' {
51+
board[i][j] = 'X';
52+
}
53+
}
54+
}
55+
}
56+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
[125][125l] |[Valid Palindrome][125] |![py]
123123
[128][128l] |[Longest Consecutive Sequence][128] |![rs]
124124
[129][129l] |[Sum Root to Leaf Numbers][129] |![py]
125+
[130][130l] |[Surrounded Regions][130] |![rs]
125126
[131][131l] |[Palindrome Partitioning][131] |![py]
126127
[133][133l] |[Clone Graph][133] |![py]
127128
[134][134l] |[Gas Station][134] |![rb]&nbsp;&nbsp;![rs]
@@ -1720,6 +1721,7 @@
17201721
[125]:Problemset/0125-Valid%20Palindrome/README.md#125-valid-palindrome
17211722
[128]:Problemset/0128-Longest%20Consecutive%20Sequence/README.md#128-longest-consecutive-sequence
17221723
[129]:Problemset/0129-Sum%20Root%20to%20Leaf%20Numbers/README.md#129-sum-root-to-leaf-numbers
1724+
[130]:Problemset/0130-Surrounded%20Regions/README.md#130-surrounded-regions
17231725
[131]:Problemset/0131-Palindrome%20Partitioning/README.md#131-palindrome-partitioning
17241726
[133]:Problemset/0133-Clone%20Graph/README.md#133-clone-graph
17251727
[134]:Problemset/0134-Gas%20Station/README.md#134-gas-station
@@ -3311,6 +3313,7 @@
33113313
[125l]:https://leetcode.com/problems/valid-palindrome/
33123314
[128l]:https://leetcode.com/problems/longest-consecutive-sequence/
33133315
[129l]:https://leetcode.com/problems/sum-root-to-leaf-numbers/
3316+
[130l]:https://leetcode.com/problems/surrounded-regions/
33143317
[131l]:https://leetcode.com/problems/palindrome-partitioning/
33153318
[133l]:https://leetcode.com/problems/clone-graph/
33163319
[134l]:https://leetcode.com/problems/gas-station/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
[125][125l] |[验证回文串][125] |![py]
123123
[128][128l] |[最长连续序列][128] |![rs]
124124
[129][129l] |[求根到叶子节点数字之和][129] |![py]
125+
[130][130l] |[被围绕的区域][130] |![rs]
125126
[131][131l] |[分割回文串][131] |![py]
126127
[133][133l] |[克隆图][133] |![py]
127128
[134][134l] |[加油站][134] |![rb]&nbsp;&nbsp;![rs]
@@ -1720,6 +1721,7 @@
17201721
[125]:Problemset/0125-Valid%20Palindrome/README_CN.md#125-验证回文串
17211722
[128]:Problemset/0128-Longest%20Consecutive%20Sequence/README_CN.md#128-最长连续序列
17221723
[129]:Problemset/0129-Sum%20Root%20to%20Leaf%20Numbers/README_CN.md#129-求根到叶子节点数字之和
1724+
[130]:Problemset/0130-Surrounded%20Regions/README_CN.md#130-被围绕的区域
17231725
[131]:Problemset/0131-Palindrome%20Partitioning/README_CN.md#131-分割回文串
17241726
[133]:Problemset/0133-Clone%20Graph/README_CN.md#133-克隆图
17251727
[134]:Problemset/0134-Gas%20Station/README_CN.md#134-加油站
@@ -3311,6 +3313,7 @@
33113313
[125l]:https://leetcode.cn/problems/valid-palindrome/
33123314
[128l]:https://leetcode.cn/problems/longest-consecutive-sequence/
33133315
[129l]:https://leetcode.cn/problems/sum-root-to-leaf-numbers/
3316+
[130l]:https://leetcode.cn/problems/surrounded-regions/
33143317
[131l]:https://leetcode.cn/problems/palindrome-partitioning/
33153318
[133l]:https://leetcode.cn/problems/clone-graph/
33163319
[134l]:https://leetcode.cn/problems/gas-station/

0 commit comments

Comments
 (0)