-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
43 lines (43 loc) · 1.62 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
class Solution {
public int[][] candyCrush(int[][] board) {
int m = board.length, n = board[0].length;
boolean run = true;
while (run) {
run = false;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n - 2; ++j) {
if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i][j + 1])
&& Math.abs(board[i][j]) == Math.abs(board[i][j + 2])) {
run = true;
board[i][j] = board[i][j + 1] = board[i][j + 2] = -Math.abs(board[i][j]);
}
}
}
for (int j = 0; j < n; ++j) {
for (int i = 0; i < m - 2; ++i) {
if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i + 1][j])
&& Math.abs(board[i][j]) == Math.abs(board[i + 2][j])) {
run = true;
board[i][j] = board[i + 1][j] = board[i + 2][j] = -Math.abs(board[i][j]);
}
}
}
if (run) {
for (int j = 0; j < n; ++j) {
int curr = m - 1;
for (int i = m - 1; i >= 0; --i) {
if (board[i][j] > 0) {
board[curr][j] = board[i][j];
--curr;
}
}
while (curr > -1) {
board[curr][j] = 0;
--curr;
}
}
}
}
return board;
}
}