1. 程式人生 > 其它 >[LeetCode] 1886. Determine Whether Matrix Can Be Obtained By Rotation

[LeetCode] 1886. Determine Whether Matrix Can Be Obtained By Rotation

Given twon x nbinary matricesmatandtarget, returntrueif it is possible to makematequal totargetbyrotatingmatin90-degree increments, orfalseotherwise.

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]andtarget[i][j]are either0or1.

判斷矩陣經輪轉後是否一致。

給你兩個大小為 n x n 的二進位制矩陣 mat 和 target 。現 以 90 度順時針輪轉 矩陣 mat 中的元素 若干次 ,如果能夠使 mat 與target 一致,返回 true ;否則,返回 false 。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/determine-whether-matrix-can-be-obtained-by-rotation
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

這道題給了一個小小的提示順時針旋轉90度,可惜我第一次做的時候沒有意識到。其實這道題跟48題很像。48題只是請你把 input matrix 順時針旋轉90度;這道題是請你判斷target是否有可能是通過將 input matrix 順時針旋轉了若干次而得來的。既然48題我們都可以不用額外空間實現,這道題也可以。我們需要把48題的方法照搬過來,只是每次旋轉90度之後都要判斷一次,一共判斷四次即可。同時注意 target matrix 有可能跟原來的 mat 一樣,不需要 rotate。

時間O(n^2)

空間O(1)

Java實現

 1 class Solution {
 2     public boolean findRotation(int[][] mat, int[][] target) {
 3         for (int i = 0; i < 4; i++) {
 4             if (Arrays.deepEquals(mat, target)) {
 5                 return true;
 6             }
 7             rotate(mat);
 8         }
 9         return false;
10     }
11 
12     private void rotate(int[][] mat) {
13         int m = mat.length;
14         for (int i = 0; i < m; i++) {
15             for (int j = i; j < m; j++) {
16                 int temp = mat[i][j];
17                 mat[i][j] = mat[j][i];
18                 mat[j][i] = temp;
19             }
20         }
21 
22         for (int i = 0; i < m; i++) {
23             for (int j = 0; j < m / 2; j++) {
24                 int temp = mat[i][j];
25                 mat[i][j] = mat[i][m - 1 - j];
26                 mat[i][m - 1 - j] = temp;
27             }
28         }
29     }
30 }

同時我也分享一下第一次做的程式碼,我是把 rotate 過後的結果 matrix 模擬出來了才判斷的。細節很不好想,而且容易錯。

 1 class Solution {
 2     public boolean findRotation(int[][] mat, int[][] target) {
 3         // corner case
 4         if (helper(target, mat)) {
 5             return true;
 6         }
 7 
 8         // normal case
 9         int[][] first = rotateOnce(mat);
10         int[][] second = rotateTwice(mat);
11         int[][] third = rotateThird(mat);
12         if (helper(target, first) || helper(target, second) || helper(target, third)) {
13             return true;
14         }
15         return false;
16     }
17 
18     private boolean helper(int[][] matrix1, int[][] matrix2) {
19         for (int i = 0; i < matrix1.length; i++) {
20             for (int j = 0; j < matrix1[0].length; j++) {
21                 if (matrix1[i][j] != matrix2[i][j]) {
22                     return false;
23                 }
24             }
25         }
26         return true;
27     }
28 
29     // rotate 90
30     private int[][] rotateOnce(int[][] mat) {
31         int len = mat.length;
32         int[][] A = new int[len][len];
33         for (int i = 0; i < mat.length; i++) {
34             for (int j = 0; j < mat[0].length; j++) {
35                 A[j][len - 1 - i] = mat[i][j];
36             }
37         }
38         return A;
39     }
40 
41     // rotate 180
42     private int[][] rotateTwice(int[][] mat) {
43         int len = mat.length;
44         int[][] B = new int[len][len];
45         for (int i = 0; i < mat.length; i++) {
46             for (int j = 0; j < mat[0].length; j++) {
47                 B[i][j] = mat[len - 1 - i][len - 1 - j];
48             }
49         }
50         return B;
51     }
52 
53     // rotate 270
54     private int[][] rotateThird(int[][] mat) {
55         int len = mat.length;
56         int[][] C = new int[len][len];
57         for (int i = 0; i < mat.length; i++) {
58             for (int j = 0; j < mat[0].length; j++) {
59                 C[len - 1 - j][i] = mat[i][j];
60                 // System.out.println("old, i " + i + " j " + j);
61                 // System.out.println("new, i " + (len - 1 - i) + " j " + (len - 1 - j));
62             }
63         }
64         // for (int i = 0; i < C.length; i++) {
65         //     System.out.println(Arrays.toString(C[i]));
66         // }
67         return C;
68     }
69 }

相關題目

48. Rotate Image

1886. Determine Whether Matrix Can Be Obtained By Rotation

LeetCode 題目總結