|
25 | 25 | */
|
26 | 26 | public class _130 {
|
27 | 27 |
|
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 { |
35 | 29 |
|
| 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 | + */ |
36 | 37 |
|
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}; |
52 | 39 |
|
| 40 | + public void solve(char[][] board) { |
| 41 | + if (board == null || board.length == 0 || board[0].length == 0) { |
| 42 | + return; |
53 | 43 | }
|
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 | + } |
57 | 58 | }
|
58 |
| - } |
59 | 59 |
|
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 | + } |
69 | 70 | }
|
70 |
| - } |
71 | 71 |
|
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 | + } |
80 | 81 | }
|
81 | 82 | }
|
82 |
| - } |
83 | 83 |
|
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 | + } |
91 | 92 | }
|
92 | 93 | }
|
93 | 94 | }
|
94 | 95 | }
|
95 |
| - |
96 | 96 | }
|
0 commit comments