Skip to content

Commit c3b0240

Browse files
committed
feat: add solutions to lc problem: No.0130.Surrounded Regions
1 parent 4d940de commit c3b0240

File tree

4 files changed

+208
-92
lines changed

4 files changed

+208
-92
lines changed

solution/0100-0199/0130.Surrounded Regions/README.md

+77-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
## 解法
4444

45-
dfs/bfs 均可
45+
DFS、BFS、并查集均可。
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

@@ -52,16 +52,91 @@ dfs/bfs 均可
5252

5353
<!-- 这里可写当前语言的特殊实现逻辑 -->
5454

55-
```python
55+
并查集。
5656

57+
```python
58+
class Solution:
59+
def solve(self, board: List[List[str]]) -> None:
60+
"""
61+
Do not return anything, modify board in-place instead.
62+
"""
63+
m, n = len(board), len(board[0])
64+
p = list(range(m * n + 1))
65+
66+
def find(x):
67+
if p[x] != x:
68+
p[x] = find(p[x])
69+
return p[x]
70+
71+
for i in range(m):
72+
for j in range(n):
73+
if board[i][j] == 'O':
74+
root = find(i * n + j)
75+
if i == 0 or j == 0 or i == m - 1 or j == n - 1:
76+
p[find(i * n + j)] = find(m * n)
77+
else:
78+
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
79+
if board[i + x][j + y] == "O":
80+
p[find(i * n + j)] = find((i + x) * n + j + y)
81+
82+
for i in range(m):
83+
for j in range(n):
84+
if find(i * n + j) != find(m * n):
85+
board[i][j] = 'X'
86+
else:
87+
board[i][j] = 'O'
5788
```
5889

5990
### **Java**
6091

6192
<!-- 这里可写当前语言的特殊实现逻辑 -->
6293

94+
并查集。
95+
6396
```java
97+
class Solution {
98+
private int[] p;
99+
private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
100+
101+
public void solve(char[][] board) {
102+
int m = board.length, n = board[0].length;
103+
p = new int[m * n + 1];
104+
for (int i = 0; i < p.length; ++i) {
105+
p[i] = i;
106+
}
107+
for (int i = 0; i < m; ++i) {
108+
for (int j = 0; j < n; ++j) {
109+
if (board[i][j] == 'O') {
110+
if (i == 0 || j == 0 || i == m - 1 || j == n - 1) {
111+
p[find(i * n + j)] = find(m * n);
112+
} else {
113+
for (int[] e : dirs) {
114+
if (board[i + e[0]][j + e[1]] == 'O') {
115+
p[find(i * n + j)] = find((i + e[0]) * n + j + e[1]);
116+
}
117+
}
118+
}
119+
}
120+
}
121+
}
122+
for (int i = 0; i < m; ++i) {
123+
for (int j = 0; j < n; ++j) {
124+
if (find(i * n + j) != find(m * n)) {
125+
board[i][j] = 'X';
126+
} else {
127+
board[i][j] = 'O';
128+
}
129+
}
130+
}
131+
}
64132

133+
private int find(int x) {
134+
if (p[x] != x) {
135+
p[x] = find(p[x]);
136+
}
137+
return p[x];
138+
}
139+
}
65140
```
66141

67142
### **TypeScript**

solution/0100-0199/0130.Surrounded Regions/README_EN.md

+76-1
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,89 @@
4141

4242
### **Python3**
4343

44-
```python
44+
Union find.
4545

46+
```python
47+
class Solution:
48+
def solve(self, board: List[List[str]]) -> None:
49+
"""
50+
Do not return anything, modify board in-place instead.
51+
"""
52+
m, n = len(board), len(board[0])
53+
p = list(range(m * n + 1))
54+
55+
def find(x):
56+
if p[x] != x:
57+
p[x] = find(p[x])
58+
return p[x]
59+
60+
for i in range(m):
61+
for j in range(n):
62+
if board[i][j] == 'O':
63+
root = find(i * n + j)
64+
if i == 0 or j == 0 or i == m - 1 or j == n - 1:
65+
p[find(i * n + j)] = find(m * n)
66+
else:
67+
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
68+
if board[i + x][j + y] == "O":
69+
p[find(i * n + j)] = find((i + x) * n + j + y)
70+
71+
for i in range(m):
72+
for j in range(n):
73+
if find(i * n + j) != find(m * n):
74+
board[i][j] = 'X'
75+
else:
76+
board[i][j] = 'O'
4677
```
4778

4879
### **Java**
4980

81+
Union find.
82+
5083
```java
84+
class Solution {
85+
private int[] p;
86+
private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
87+
88+
public void solve(char[][] board) {
89+
int m = board.length, n = board[0].length;
90+
p = new int[m * n + 1];
91+
for (int i = 0; i < p.length; ++i) {
92+
p[i] = i;
93+
}
94+
for (int i = 0; i < m; ++i) {
95+
for (int j = 0; j < n; ++j) {
96+
if (board[i][j] == 'O') {
97+
if (i == 0 || j == 0 || i == m - 1 || j == n - 1) {
98+
p[find(i * n + j)] = find(m * n);
99+
} else {
100+
for (int[] e : dirs) {
101+
if (board[i + e[0]][j + e[1]] == 'O') {
102+
p[find(i * n + j)] = find((i + e[0]) * n + j + e[1]);
103+
}
104+
}
105+
}
106+
}
107+
}
108+
}
109+
for (int i = 0; i < m; ++i) {
110+
for (int j = 0; j < n; ++j) {
111+
if (find(i * n + j) != find(m * n)) {
112+
board[i][j] = 'X';
113+
} else {
114+
board[i][j] = 'O';
115+
}
116+
}
117+
}
118+
}
51119

120+
private int find(int x) {
121+
if (p[x] != x) {
122+
p[x] = find(p[x]);
123+
}
124+
return p[x];
125+
}
126+
}
52127
```
53128

54129
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,43 @@
11
class Solution {
2-
3-
/**
4-
* 坐标点
5-
*/
6-
private class Point {
7-
int x;
8-
int y;
9-
10-
Point(int x, int y) {
11-
this.x = x;
12-
this.y = y;
13-
}
14-
}
2+
private int[] p;
3+
private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
154

165
public void solve(char[][] board) {
17-
if (board == null || board.length < 3 || board[0].length < 3) {
18-
return;
19-
}
20-
21-
int m = board.length;
22-
int n = board[0].length;
23-
24-
// top & bottom
25-
for (int i = 0; i < n; ++i) {
26-
bfs(board, 0, i);
27-
bfs(board, m - 1, i);
6+
int m = board.length, n = board[0].length;
7+
p = new int[m * n + 1];
8+
for (int i = 0; i < p.length; ++i) {
9+
p[i] = i;
2810
}
29-
30-
// left & right
31-
for (int i = 1; i < m - 1; ++i) {
32-
bfs(board, i, 0);
33-
bfs(board, i, n - 1);
34-
}
35-
3611
for (int i = 0; i < m; ++i) {
3712
for (int j = 0; j < n; ++j) {
3813
if (board[i][j] == 'O') {
39-
board[i][j] = 'X';
40-
} else if (board[i][j] == 'Y') {
41-
board[i][j] = 'O';
14+
if (i == 0 || j == 0 || i == m - 1 || j == n - 1) {
15+
p[find(i * n + j)] = find(m * n);
16+
} else {
17+
for (int[] e : dirs) {
18+
if (board[i + e[0]][j + e[1]] == 'O') {
19+
p[find(i * n + j)] = find((i + e[0]) * n + j + e[1]);
20+
}
21+
}
22+
}
4223
}
4324
}
4425
}
45-
}
46-
47-
/**
48-
* 广度优先搜索
49-
* @param board
50-
* @param i
51-
* @param j
52-
*/
53-
private void bfs(char[][] board, int i, int j) {
54-
Queue<Point> queue = new LinkedList<>();
55-
if (isValid(board, i, j)) {
56-
// 遇到'O',修改为'Y'
57-
board[i][j] = 'Y';
58-
queue.offer(new Point(i, j));
59-
}
60-
61-
while (!queue.isEmpty()) {
62-
Point p = queue.poll();
63-
// 获取下一层所有有效坐标点,并加入队列
64-
List<Point> points = getNextLevelValidPoints(board, p.x, p.y);
65-
for (Point point : points) {
66-
queue.offer(point);
26+
for (int i = 0; i < m; ++i) {
27+
for (int j = 0; j < n; ++j) {
28+
if (find(i * n + j) != find(m * n)) {
29+
board[i][j] = 'X';
30+
} else {
31+
board[i][j] = 'O';
32+
}
6733
}
6834
}
69-
7035
}
7136

72-
/**
73-
* 获取下一层所有有效坐标点,将这些坐标点修改为 'Y' 并返回
74-
* @param board
75-
* @param i
76-
* @param j
77-
* @return list
78-
*/
79-
private List<Point> getNextLevelValidPoints(char[][] board, int i, int j) {
80-
List<Point> points = new ArrayList<>();
81-
Point[] arr = new Point[] { new Point(i - 1, j), new Point(i + 1, j), new Point(i, j - 1),
82-
new Point(i, j + 1) };
83-
for (Point point : arr) {
84-
if (isValid(board, point.x, point.y)) {
85-
board[point.x][point.y] = 'Y';
86-
points.add(point);
87-
}
37+
private int find(int x) {
38+
if (p[x] != x) {
39+
p[x] = find(p[x]);
8840
}
89-
return points;
41+
return p[x];
9042
}
91-
92-
/**
93-
* 判断坐标是否有效
94-
* @param board
95-
* @param i
96-
* @param j
97-
* @return boolean
98-
*/
99-
private boolean isValid(char[][] board, int i, int j) {
100-
int m = board.length;
101-
int n = board[0].length;
102-
// 当前坐标对应的值是'O',才算有效
103-
return i >= 0 && i <= m - 1 && j >= 0 && j <= n - 1 && board[i][j] == 'O';
104-
}
105-
10643
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def solve(self, board: List[List[str]]) -> None:
3+
"""
4+
Do not return anything, modify board in-place instead.
5+
"""
6+
m, n = len(board), len(board[0])
7+
p = list(range(m * n + 1))
8+
9+
def find(x):
10+
if p[x] != x:
11+
p[x] = find(p[x])
12+
return p[x]
13+
14+
for i in range(m):
15+
for j in range(n):
16+
if board[i][j] == 'O':
17+
if i == 0 or j == 0 or i == m - 1 or j == n - 1:
18+
p[find(i * n + j)] = find(m * n)
19+
else:
20+
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
21+
if board[i + x][j + y] == "O":
22+
p[find(i * n + j)] = find((i + x) * n + j + y)
23+
24+
for i in range(m):
25+
for j in range(n):
26+
if find(i * n + j) != find(m * n):
27+
board[i][j] = 'X'
28+
else:
29+
board[i][j] = 'O'

0 commit comments

Comments
 (0)