-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.txt
68 lines (52 loc) · 912 Bytes
/
demo.txt
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
1 2 3
4 5 6
7 8 9
m x n
3 x 3
7 4 1
8 5 2
9 6 3
i,j i,j i = 0 to m-1
1. 0,0 0,2 j = n - 1 - i
2. 0,1 1,2
3. 0,2 2,2
4. 1,0 0,1
5. 1,1 1,1
6. 1,2 2,1
m x n = 3 x 4
1 2 3 4
5 6 7 8
9 10 11 12
m x n = 4 x 3
9 5 1
10 6 2
11 7 3
12 8 4
0 1 2 3 4
0 --> 1 2 3 4 5
1 --> 5 6 7 8 10
2 --> 11 12 13 14 15
original (3 x 5) m x n
0 1 2
0 --> 11 5 1
1 --> 12 6 2
2 --> 13 7 3
3 --> 14 8 4
4 --> 15 10 5
rotated (5 x 3) n x m
mapping (i,j) ---> (j,m - i -1)
1. 0,0 --> 0,2
2. 0,1 --> 1,2
3. 0,2 --> 2,2
4. 0,3 --> 3,2
5. 0,4 --> 4,2
public void rotate(int[][] mat){
int m = mat.length;
int n = mat[0].length;
int[][] temp = new int[n][m];
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
temp[j][m - i -1] = mat[i][j];
}
}
}