forked from kishanrajput23/leetcode-solutions-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotate_Matrix_by_90_degrees.java
More file actions
35 lines (30 loc) · 967 Bytes
/
Rotate_Matrix_by_90_degrees.java
File metadata and controls
35 lines (30 loc) · 967 Bytes
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
//You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
//https://leetcode.com/problems/rotate-image/
public class Rotate_Matrix_by_90_degrees {
public static int[][] transpose(int [][] ary){
int rows=ary.length;
for (int i=0;i<rows;i++){
for (int j=0;j<=i;j++){
int temp=ary[i][j];
ary[i][j]=ary[j][i];
ary[j][i]=temp;
}
}
return ary;
}
public static int[][] rotateCol(int [][] ary){
int rows=ary.length,cols=ary[0].length;
for (int i=0;i<rows;i++){
for (int j=0;j<cols/2;j++){
int temp=ary[i][j];
ary[i][j]=ary[i][cols-1-j];
ary[i][cols-1-j]=temp;
}
}
return ary;
}
public static void rotate(int[][] matrix) {
matrix=transpose(matrix);
matrix=rotateCol(matrix);
}
}