zoukankan      html  css  js  c++  java
  • [LeetCode] 1886. Determine Whether Matrix Can Be Obtained By Rotation

    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.

    判断矩阵经轮转后是否一致。

    给你两个大小为 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 题目总结

  • 相关阅读:
    Java数组
    Java单例设计模式
    Java 14 祭出代码简化大器,Lombok 要被干掉了?
    来,教你去掉了烦人的 !=null
    Java 最坑爹的 10 大功能点!
    高级 Java 必须突破的 10 个知识点!
    不用找了,基于 Redis 的分布式锁实战来了!
    Spring 常犯的十大错误,打死都不要犯!
    JVM 与 Linux 的内存关系详解
    Java 中的 T,E,K,V, 别傻傻分不清楚!
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14893005.html
Copyright © 2011-2022 走看看