forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotateimage.java
More file actions
executable file
·40 lines (36 loc) · 1.09 KB
/
rotateimage.java
File metadata and controls
executable file
·40 lines (36 loc) · 1.09 KB
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
public class Solution {
private void rotatelayer(int[][] matrix, int x1, int x2){
}
public void rotate(int[][] matrix) {
// Start typing your Java solution below
// DO NOT write main() function
int m = matrix.length;
if(m==0) return;
int x1 = 0;
int x2 = m-1;
int tmp = 0;
while(x1<x2){
int n = x2-x1;
//[x1,x1][x1,x2] -- [x1,x2][x2,x2]
for(int i=0;i<n;i++){
tmp = matrix[x1][x1+i];
matrix[x1][x1+i] = matrix[x1+i][x2];
matrix[x1+i][x2] = tmp;
}
//[x2,x2][x2,x1]
for(int i=0;i<n;i++){
tmp = matrix[x1][x1+i];
matrix[x1][x1+i] = matrix[x2][x2-i];
matrix[x2][x2-i] = tmp;
}
//[x2,x1][x1,x1]
for(int i=0;i<n;i++){
tmp = matrix[x1][x1+i];
matrix[x1][x1+i] = matrix[x2-i][x1];
matrix[x2-i][x1] = tmp;
}
x1++;
x2--;
}
}
}