-
Notifications
You must be signed in to change notification settings - Fork 0
/
P7.java
42 lines (41 loc) · 1.37 KB
/
P7.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
import java.util.*;
public class P7 {
public static void rotateMatrix(ArrayList<ArrayList<Integer>> mat, int n, int m) {
int row = 0;
int col = 0;
int maxRow = n;
int maxCol = m;
int previous, current;
while (row < maxRow - 1 && col < maxCol - 1) {
previous = mat.get(row + 1).get(col);
for (int i = col; i < maxCol; i++) {
current = mat.get(row).get(i);// 2
mat.get(row).set(i, previous);
previous = current;
}
row++;
for (int i = row; i < maxRow; i++) {
current = mat.get(i).get(maxCol - 1);
mat.get(i).set(maxCol - 1, previous);
previous = current;
}
maxCol--;
if (row < maxRow) {
for (int i = maxCol - 1; i >= col; i--) {
current = mat.get(maxRow - 1).get(i);
mat.get(maxRow - 1).set(i, previous);
previous = current;
}
}
maxRow--;
if (col < maxCol) {
for (int i = maxRow - 1; i >= row; i--) {
current = mat.get(i).get(col);
mat.get(i).set(col, previous);
previous = current;
}
}
col++;
}
}
}