-
Notifications
You must be signed in to change notification settings - Fork 0
/
1848.minimum-distance-to-the-target-element.java
50 lines (39 loc) · 1.22 KB
/
1848.minimum-distance-to-the-target-element.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
44
45
46
47
48
49
50
class Solution {
public char[][] rotateTheBox(char[][] box) {
char[][] rotateBox = new char[box[0].length][box.length];
int i = 0;
while(i<box.length){
int j = box[0].length-1;
int end = box[0].length-1;
while(j>=0 && end >= 0){
if(box[i][end] == '*'){
end--;
continue;
}
else
if(box[i][j] == '#'){
swap(box, i, j, end);
end--;
}else if(box[i][j] == '*'){
end = j-1;
}
j--;
}
i++;
}
rotate(box, rotateBox);
return rotateBox;
}
public void rotate(char[][] box, char[][] rotateBox){
for(int i=0;i<box[0].length;i++){
for(int j=0;j<box.length;j++){
rotateBox[i][box.length-j-1] = box[j][i];
}
}
}
public void swap(char[][] box, int i, int j, int end){
char tmp = box[i][j];
box[i][j] = box[i][end];
box[i][end] = tmp;
}
}