forked from JojoYang666/Leetcode-1-300
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_130_SurroundedRegions.java
158 lines (135 loc) · 4.44 KB
/
_130_SurroundedRegions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package leetcode_1To300;
import java.util.LinkedList;
import java.util.Queue;
/**
* 本代码来自 Cspiration,由 @Cspiration 提供
* 题目来源:http://leetcode.com
* - Cspiration 致力于在 CS 领域内帮助中国人找到工作,让更多海外国人受益
* - 现有课程:Leetcode Java 版本视频讲解(1-900题)(上)(中)(下)三部
* - 算法基础知识(上)(下)两部;题型技巧讲解(上)(下)两部
* - 节省刷题时间,效率提高2-3倍,初学者轻松一天10题,入门者轻松一天20题
* - 讲师:Edward Shi
* - 官方网站:https://cspiration.com
* - 版权所有,转发请注明出处
*/
public class _130_SurroundedRegions {
/**
* 130. Surrounded Regions
* Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region.
For example,
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
time : O(m * n)
space : O(n)
* @param board
*/
public void solve(char[][] board){
if (board == null || board.length == 0 || board[0].length == 0) return;
int m = board.length - 1;
int n = board[0].length - 1;
for (int i = 0; i <= m; i++) {
if (board[i][0] == 'O') {
dfs(board, i, 0);
}
if (board[i][n] == 'O') {
dfs(board, i, n);
}
}
for (int i = 0; i <= n; i++) {
if (board[0][i] == 'O') {
dfs(board, 0, i);
}
if (board[m][i] == 'O') {
dfs(board, m, i);
}
}
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == '1') {
board[i][j] = 'O';
}
}
}
}
private void dfs(char[][] board, int i, int j) {
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] != 'O') {
return;
}
board[i][j] = '1';
dfs(board, i, j + 1);
dfs(board, i, j - 1);
dfs(board, i + 1, j);
dfs(board, i - 1, j);
}
class Point {
int x;
int y;
Point(int a, int b) {
x = a;
y = b;
}
}
public void solve2(char[][] board) {
if (board == null || board.length == 0 || board[0].length == 0) return;
int m = board.length - 1;
int n = board[0].length - 1;
Queue<Point> queue = new LinkedList<>();
//get all 'O's on the edge first
for (int r = 0; r <= m; r++) {
if (board[r][0] == 'O') {
board[r][0] = '1';
queue.add(new Point(r, 0));
}
if (board[r][n] == 'O') {
board[r][n] = '1';
queue.add(new Point(r, n));
}
}
for (int i = 0; i < n; i++){
if (board[0][i] == 'O') {
board[0][i] = '1';
queue.add(new Point(0, i));
}
if (board[m][i] == 'O') {
board[m][i] = '1';
queue.add(new Point(m, i));
}
}
while (!queue.isEmpty()) {
Point p = queue.poll();
int row = p.x;
int col = p.y;
if (row - 1 >= 0 && board[row - 1][col] == 'O') {
board[row - 1][col] = '1';
queue.add(new Point(row - 1, col));
}
if (row + 1 < m && board[row + 1][col] == 'O') {
board[row + 1][col] = '1'; queue.add(new Point(row + 1, col));
}
if (col - 1 >= 0 && board[row][col - 1] == 'O') {
board[row][col - 1] = '1';
queue.add(new Point(row, col - 1));
}
if (col + 1 < n && board[row][col + 1] == 'O') {
board[row][col + 1] = '1';
queue.add(new Point(row, col + 1));
}
}
for (int i = 0; i<= m; i++){
for (int j=0; j<= n; j++){
if (board[i][j] == 'O') board[i][j] = 'X';
if (board[i][j] == '1') board[i][j] = 'O';
}
}
}
}