|
| 1 | +# [1886. 判断矩阵经轮转后是否一致](https://leetcode-cn.com/problems/determine-whether-matrix-can-be-obtained-by-rotation) |
| 2 | + |
| 3 | +[English Version](/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>给你两个大小为 <code>n x n</code> 的二进制矩阵 <code>mat</code> 和 <code>target</code> 。现<strong> 以 90 度顺时针轮转 </strong>矩阵 <code>mat</code> 中的元素 <strong>若干次</strong> ,如果能够使 <code>mat</code> 与 <code>target</code> 一致,返回 <code>true</code> ;否则,返回<em> </em><code>false</code><em> 。</em></p> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | + |
| 13 | +<p><strong>示例 1:</strong></p> |
| 14 | +<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/images/grid3.png" style="width: 301px; height: 121px;" /> |
| 15 | +<pre> |
| 16 | +<strong>输入:</strong>mat = [[0,1],[1,0]], target = [[1,0],[0,1]] |
| 17 | +<strong>输出:</strong>true |
| 18 | +<strong>解释:</strong>顺时针轮转 90 度一次可以使 mat 和 target 一致。 |
| 19 | +</pre> |
| 20 | + |
| 21 | +<p><strong>示例 2:</strong></p> |
| 22 | +<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/images/grid4.png" style="width: 301px; height: 121px;" /> |
| 23 | +<pre> |
| 24 | +<strong>输入:</strong>mat = [[0,1],[1,1]], target = [[1,0],[0,1]] |
| 25 | +<strong>输出:</strong>false |
| 26 | +<strong>解释:</strong>无法通过轮转矩阵中的元素使 equal 与 target 一致。 |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p><strong>示例 3:</strong></p> |
| 30 | +<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/images/grid4-1.png" style="width: 661px; height: 184px;" /> |
| 31 | +<pre> |
| 32 | +<strong>输入:</strong>mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]] |
| 33 | +<strong>输出:</strong>true |
| 34 | +<strong>解释:</strong>顺时针轮转 90 度两次可以使 mat 和 target 一致。 |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | + |
| 39 | +<p><strong>提示:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>n == mat.length == target.length</code></li> |
| 43 | + <li><code>n == mat[i].length == target[i].length</code></li> |
| 44 | + <li><code>1 <= n <= 10</code></li> |
| 45 | + <li><code>mat[i][j]</code> 和 <code>target[i][j]</code> 不是 <code>0</code> 就是 <code>1</code></li> |
| 46 | +</ul> |
| 47 | + |
| 48 | + |
| 49 | +## 解法 |
| 50 | + |
| 51 | +<!-- 这里可写通用的实现逻辑 --> |
| 52 | + |
| 53 | +旋转矩阵,判断矩阵是否一致。 |
| 54 | + |
| 55 | +<!-- tabs:start --> |
| 56 | + |
| 57 | +### **Python3** |
| 58 | + |
| 59 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 60 | + |
| 61 | +```python |
| 62 | +class Solution: |
| 63 | + def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool: |
| 64 | + def rotate(matrix): |
| 65 | + n = len(matrix) |
| 66 | + for i in range(n // 2): |
| 67 | + for j in range(i, n - 1 - i): |
| 68 | + t = matrix[i][j] |
| 69 | + matrix[i][j] = matrix[n - j - 1][i] |
| 70 | + matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1] |
| 71 | + matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1] |
| 72 | + matrix[j][n - i - 1] = t |
| 73 | + for _ in range(4): |
| 74 | + if mat == target: |
| 75 | + return True |
| 76 | + rotate(mat) |
| 77 | + return False |
| 78 | +``` |
| 79 | + |
| 80 | +### **Java** |
| 81 | + |
| 82 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 83 | + |
| 84 | +```java |
| 85 | +class Solution { |
| 86 | + public boolean findRotation(int[][] mat, int[][] target) { |
| 87 | + int times = 4; |
| 88 | + while (times-- > 0) { |
| 89 | + if (equals(mat, target)) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + rotate(mat); |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + private void rotate(int[][] matrix) { |
| 98 | + int n = matrix.length; |
| 99 | + for (int i = 0; i < n / 2; ++i) { |
| 100 | + for (int j = i; j < n - 1 - i; ++j) { |
| 101 | + int t = matrix[i][j]; |
| 102 | + matrix[i][j] = matrix[n - j - 1][i]; |
| 103 | + matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]; |
| 104 | + matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]; |
| 105 | + matrix[j][n - i - 1] = t; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private boolean equals(int[][] nums1, int[][] nums2) { |
| 111 | + int n = nums1.length; |
| 112 | + for (int i = 0; i < n; ++i) { |
| 113 | + for (int j = 0; j < n; ++j) { |
| 114 | + if (nums1[i][j] != nums2[i][j]) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return true; |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### **...** |
| 125 | + |
| 126 | +``` |
| 127 | +
|
| 128 | +``` |
| 129 | + |
| 130 | +<!-- tabs:end --> |
0 commit comments