forked from chuyi2007/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateImage.java
More file actions
35 lines (33 loc) · 1.18 KB
/
RotateImage.java
File metadata and controls
35 lines (33 loc) · 1.18 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
//O(N^2)
public class Solution {
public void rotate(int[][] matrix) {
// Start typing your Java solution below
// DO NOT write main() function
//extraSpace(matrix);
constantSpace(matrix);
}
public void constantSpace(int[][] matrix){
int n = matrix.length;
for(int i = 0; i < n; ++i)
for(int j = i; j < n - i - 1; ++j){
int tmp = matrix[i][j];
matrix[i][j] = matrix[n - j - 1][i];
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
matrix[j][n - i - 1] = tmp;
}
}
public void extraSpace(int[][] matrix){
int[][] newMatrix = new int[matrix.length][matrix.length];
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix.length; j++){
newMatrix[j][matrix.length-i - 1] = matrix[i][j];
}
}
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix.length; j++){
matrix[i][j] = newMatrix[i][j];
}
}
}
}