-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1p7.java
More file actions
45 lines (42 loc) · 1.38 KB
/
Copy pathq1p7.java
File metadata and controls
45 lines (42 loc) · 1.38 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
41
42
43
44
45
import java.util.*;
import java.lang.*;
public class q1p7{
public static void main(String[] args){
int[][] matrix = {{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4}};
for(int i = 0; i<matrix.length; i++){
for(int j = 0; j<matrix.length; j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println("");
}
rotate(matrix);
for(int i = 0; i<matrix.length; i++){
for(int j = 0; j<matrix.length; j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println("");
}
}
// T: O(n^2)
// S: O(1)
// A: layer by layer
public static void rotate(int[][] matrix){
int len = matrix.length;
for(int layer = 0; layer < len/2; layer++){
int first = layer;
int last = len - 1 - layer;
for(int i = first; i < last; i++){
int offset = i - first;
int top = matrix[first][i];
// left -> top
matrix[first][i] = matrix[last-offset][first];
// bottom -> left
matrix[last-offset][first] = matrix[last][last-offset];
// right -> bottom
matrix[last][last-offset] = matrix[i][last];
// top -> right
matrix[i][last] = top;
}
}
}
}