Skip to content

Commit 4f62ae6

Browse files
refactor 130
1 parent bf0a29e commit 4f62ae6

File tree

1 file changed

+53
-53
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+53
-53
lines changed

src/main/java/com/fishercoder/solutions/_130.java

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,72 +25,72 @@
2525
*/
2626
public class _130 {
2727

28-
/**
29-
* I won't call this problem hard, it's just confusing, you'll definitely want to clarify what the problem means before coding.
30-
* This problem eactually means:
31-
* any grid that is 'O' but on the four edges, will never be marked to 'X';
32-
* furthermore, any grid that is 'O' and that is connected with the above type of 'O' will never be marked to 'X' as well;
33-
* only all other nodes that has any one direct neighbor that is an 'X' will be marked to 'X'.
34-
*/
28+
public static class Solution1 {
3529

30+
/**
31+
* I won't call this problem hard, it's just confusing, you'll definitely want to clarify what
32+
* the problem means before coding. This problem eactually means: any grid that is 'O' but on
33+
* the four edges, will never be marked to 'X'; furthermore, any grid that is 'O' and that is
34+
* connected with the above type of 'O' will never be marked to 'X' as well; only all other
35+
* nodes that has any one direct neighbor that is an 'X' will be marked to 'X'.
36+
*/
3637

37-
int[] dirs = new int[]{0, 1, 0, -1, 0};
38-
39-
public void solve(char[][] board) {
40-
if (board == null || board.length == 0 || board[0].length == 0) {
41-
return;
42-
}
43-
int m = board.length;
44-
int n = board[0].length;
45-
Queue<int[]> queue = new LinkedList();
46-
//check first row and last row and mark all those '0' on these two rows to be '+' to let them be different from other 'O',
47-
//at the same time, we put them into the queue to get ready for a BFS to mark all those adjacent 'O' nodes to '+' as well
48-
for (int j = 0; j < n; j++) {
49-
if (board[0][j] == 'O') {
50-
board[0][j] = '+';
51-
queue.offer(new int[]{0, j});
38+
int[] dirs = new int[] {0, 1, 0, -1, 0};
5239

40+
public void solve(char[][] board) {
41+
if (board == null || board.length == 0 || board[0].length == 0) {
42+
return;
5343
}
54-
if (board[m - 1][j] == 'O') {
55-
board[m - 1][j] = '+';
56-
queue.offer(new int[]{m - 1, j});
44+
int m = board.length;
45+
int n = board[0].length;
46+
Queue<int[]> queue = new LinkedList();
47+
//check first row and last row and mark all those '0' on these two rows to be '+' to let them be different from other 'O',
48+
//at the same time, we put them into the queue to get ready for a BFS to mark all those adjacent 'O' nodes to '+' as well
49+
for (int j = 0; j < n; j++) {
50+
if (board[0][j] == 'O') {
51+
board[0][j] = '+';
52+
queue.offer(new int[] {0, j});
53+
}
54+
if (board[m - 1][j] == 'O') {
55+
board[m - 1][j] = '+';
56+
queue.offer(new int[] {m - 1, j});
57+
}
5758
}
58-
}
5959

60-
//check first column and last column too
61-
for (int i = 0; i < m; i++) {
62-
if (board[i][0] == 'O') {
63-
board[i][0] = '+';
64-
queue.offer(new int[]{i, 0});
65-
}
66-
if (board[i][n - 1] == 'O') {
67-
board[i][n - 1] = '+';
68-
queue.offer(new int[]{i, n - 1});
60+
//check first column and last column too
61+
for (int i = 0; i < m; i++) {
62+
if (board[i][0] == 'O') {
63+
board[i][0] = '+';
64+
queue.offer(new int[] {i, 0});
65+
}
66+
if (board[i][n - 1] == 'O') {
67+
board[i][n - 1] = '+';
68+
queue.offer(new int[] {i, n - 1});
69+
}
6970
}
70-
}
7171

72-
while (!queue.isEmpty()) {
73-
int[] curr = queue.poll();
74-
for (int i = 0; i < 4; i++) {
75-
int x = curr[0] + dirs[i];
76-
int y = curr[1] + dirs[i + 1];
77-
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') {
78-
board[x][y] = '+';
79-
queue.offer(new int[]{x, y});
72+
while (!queue.isEmpty()) {
73+
int[] curr = queue.poll();
74+
for (int i = 0; i < 4; i++) {
75+
int x = curr[0] + dirs[i];
76+
int y = curr[1] + dirs[i + 1];
77+
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') {
78+
board[x][y] = '+';
79+
queue.offer(new int[] {x, y});
80+
}
8081
}
8182
}
82-
}
8383

84-
//now we can safely mark all other 'O' to 'X', also remember to put those '+' back to 'O'
85-
for (int i = 0; i < m; i++) {
86-
for (int j = 0; j < n; j++) {
87-
if (board[i][j] == 'O') {
88-
board[i][j] = 'X';
89-
} else if (board[i][j] == '+') {
90-
board[i][j] = 'O';
84+
//now we can safely mark all other 'O' to 'X', also remember to put those '+' back to 'O'
85+
for (int i = 0; i < m; i++) {
86+
for (int j = 0; j < n; j++) {
87+
if (board[i][j] == 'O') {
88+
board[i][j] = 'X';
89+
} else if (board[i][j] == '+') {
90+
board[i][j] = 'O';
91+
}
9192
}
9293
}
9394
}
9495
}
95-
9696
}

0 commit comments

Comments
 (0)