-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetermine-whether-matrix-can-be-obtained-by-rotation.java
58 lines (58 loc) · 1.72 KB
/
determine-whether-matrix-can-be-obtained-by-rotation.java
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
class Solution {
public boolean findRotation(int[][] matrix, int[][] target) {
//check upto 3 times coz we can rotate any matrix by 90degrees atmost 3 times only
for(int k=0;k<4;k++){
matrix=transpose(matrix);
matrix=revrow(matrix);
if(eqq(matrix,target)==true){
return true;
}
}
return false;
}
public static int[][] transpose(int[][] matrix){
// int[][] ans=new int[matrix[0].length][matrix.length];
// for(int i=0;i<matrix.length;i++){
// for(int j=0;j<matrix[0].length;j++){
// ans[i][j]=matrix[j][i];
// }
// }
// return ans;
int n=matrix.length;
int temp=0;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
return matrix;
}
public static int[][] revrow(int[][] matrix){
int n=matrix.length;
for(int i=0;i<n;i++){
int start = 0;
int end = n-1;
while (start < end) {
int temp1 = matrix[i][start];
matrix[i][start] = matrix[i][end];
matrix[i][end] = temp1;
start++;
end--;
}
}
return matrix;
}
public static boolean eqq(int[][] matrix, int[][] target){
int n=matrix.length;
for(int i=0;i<n;i++){
for(int j=0;j<matrix[0].length;j++){
if(target[i][j]!=matrix[i][j]){
return false;
}
}
}
return true;
}
}