-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
47 lines (47 loc) · 1.5 KB
/
Solution.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
public class Solution {
public void solve(char[][] board) {
if (board!=null&&board.length!=0&&board[0].length!=0) {
int row=board.length;
int col=board[0].length;
List<Integer> test=new ArrayList<Integer>();
for (int i = 0; i < row; i++) {
if (board[i][0]=='O') {
test.add(i*col);
}
if (board[i][col-1]=='O') {
test.add(i*col+col-1);
}
}
for (int i = 0; i < col; i++) {
if (board[0][i]=='O') {
test.add(i);
}
if (board[row-1][i]=='O') {
test.add((row-1)*col+i);
}
}
int a=0;
while (a<test.size()) {
int key=test.get(a++);
int x=key/col;
int y=key%col;
if (x>=0&&x<row&&y>=0&&y<col&&'O'==board[x][y]) {
board[x][y]='A';
test.add(key-col);
test.add(key+col);
test.add(key-1);
test.add(key+1);
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j]!='A') {
board[i][j]='X';
}else{
board[i][j]='O';
}
}
}
}
}
}