Skip to content

Commit fd87397

Browse files
1886 matrix very good rotation transpose in/not inplace functions
Given two n x n binary matrices mat and target, return true if it is possible to make mat equal to target by rotating mat in 90-degree increments, or false otherwise. Example 1: Input: mat = [[0,1],[1,0]], target = [[1,0],[0,1]] Output: true Explanation: We can rotate mat 90 degrees clockwise to make mat equal target. Example 2: Input: mat = [[0,1],[1,1]], target = [[1,0],[0,1]] Output: false Explanation: It is impossible to make mat equal to target by rotating mat. Example 3: Input: mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]] Output: true Explanation: We can rotate mat 90 degrees clockwise two times to make mat equal target. Constraints: n == mat.length == target.length n == mat[i].length == target[i].length 1 <= n <= 10 mat[i][j] and target[i][j] are either 0 or 1. Seen this question in a real interview before? 1/5 Yes No Accepted 70.7K Submissions 124K Acceptance Rate 57.0% Topics Array Matrix Companies Hint 1 What is the maximum number of rotations you have to check? Hint 2 Is there a formula you can use to rotate a matrix 90 degrees?
1 parent 0ce63bf commit fd87397

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Solution {
2+
public boolean findRotation(int[][] matrix, int[][] target) {
3+
//check upto 3 times coz we can rotate any matrix by 90degrees atmost 3 times only
4+
for(int k=0;k<4;k++){
5+
matrix=transpose(matrix);
6+
matrix=revrow(matrix);
7+
if(eqq(matrix,target)==true){
8+
return true;
9+
}
10+
}
11+
return false;
12+
}
13+
public static int[][] transpose(int[][] matrix){
14+
// int[][] ans=new int[matrix[0].length][matrix.length];
15+
// for(int i=0;i<matrix.length;i++){
16+
// for(int j=0;j<matrix[0].length;j++){
17+
// ans[i][j]=matrix[j][i];
18+
// }
19+
// }
20+
// return ans;
21+
int n=matrix.length;
22+
int temp=0;
23+
for(int i=0;i<n-1;i++){
24+
for(int j=i+1;j<n;j++){
25+
temp = matrix[i][j];
26+
matrix[i][j] = matrix[j][i];
27+
matrix[j][i] = temp;
28+
}
29+
}
30+
return matrix;
31+
}
32+
public static int[][] revrow(int[][] matrix){
33+
int n=matrix.length;
34+
for(int i=0;i<n;i++){
35+
int start = 0;
36+
int end = n-1;
37+
while (start < end) {
38+
int temp1 = matrix[i][start];
39+
matrix[i][start] = matrix[i][end];
40+
matrix[i][end] = temp1;
41+
start++;
42+
end--;
43+
}
44+
}
45+
return matrix;
46+
}
47+
public static boolean eqq(int[][] matrix, int[][] target){
48+
int n=matrix.length;
49+
for(int i=0;i<n;i++){
50+
for(int j=0;j<matrix[0].length;j++){
51+
if(target[i][j]!=matrix[i][j]){
52+
return false;
53+
}
54+
}
55+
}
56+
return true;
57+
}
58+
}

0 commit comments

Comments
 (0)