-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
76 lines (51 loc) · 1.8 KB
/
Solution.java
File metadata and controls
76 lines (51 loc) · 1.8 KB
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
package leetcode.surroundedRegions;
class Solution {
public void solve(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
char value = board[i][j];
if (value == 'X') {
continue;
}
if (isAround(board, i, j)) {
board[i][j] = 'X';
}
}
}
}
public boolean isAround(char[][] board, int X, int y) {
return haveInX(board, X, y, -1) &&
haveInX(board, X, y, 1) &&
haveInY(board, X, y, -1) &&
haveInY(board, X, y, 1);
}
private boolean haveInX(char[][] board, int x, int y, int move) {
int colls = board.length;
int rows = board[0].length;
int newX = x + move;
if (newX < 0 || y < 0 || newX >= colls || y >= rows) return false;
char value = board[newX][y];
if (value == 'X') return true;
return haveInX(board, newX, y, move);
}
private boolean haveInY(char[][] board, int x, int y, int move) {
int colls = board.length;
int rows = board[0].length;
int newY = y + move;
if (x < 0 || newY < 0 || x >= colls || newY >= rows) return false;
char value = board[x][newY];
if (value == 'X') return true;
return haveInX(board, x, newY, move);
}
public static void main(String[] args) {
char[][] board = {
{'O', 'X', 'X', 'O', 'X'},
{'X', 'O', 'O', 'X', 'O'},
{'X', 'O', 'X', 'O', 'X'},
{'O', 'X', 'O', 'O', 'O'},
{'X', 'X', 'O', 'X', 'O'}
};
Solution solution = new Solution();
solution.solve(board);
}
}