-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathrotateTheBox.java
37 lines (32 loc) · 1.01 KB
/
rotateTheBox.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
class Solution {
public char[][] rotateTheBox(char[][] box) {
int m = box.length;
int n = box[0].length;
char[][] matrix = new char[n][m];
turnBox(box, matrix, m, n);
turnGravity(matrix, n, m);
return matrix;
}
public void turnGravity(char[][] matrix, int m, int n){
for(int c = 0; c < n; c++){
int last = m-1;
for(int r = m-1; r >= 0; r--){
char val = matrix[r][c];
if(val == '.') continue;
if(val == '*') last = r-1;
if(val == '#') {
matrix[r][c] = '.';
matrix[last][c] = '#';
last--;
}
}
}
}
public void turnBox(char[][] box, char[][] matrix, int m, int n){
for(int i = 0, c = m-1; i < m; i++,c--) {
for(int j = 0, r = 0; j < n; j++, r++) {
matrix[r][c] = box[i][j];
}
}
}
}