|
| 1 | +--- |
| 2 | +title: 1886.判断矩阵经轮转后是否一致:模拟 |
| 3 | +date: 2026-03-22 23:47:38 |
| 4 | +tags: [题解, LeetCode, 简单, 数组, 矩阵] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +index_img: https://assets.leetcode.com/uploads/2021/05/20/grid3.png |
| 7 | +--- |
| 8 | + |
| 9 | +# 【LetMeFly】1886.判断矩阵经轮转后是否一致:模拟 |
| 10 | + |
| 11 | +力扣题目链接:[https://leetcode.cn/problems/determine-whether-matrix-can-be-obtained-by-rotation/](https://leetcode.cn/problems/determine-whether-matrix-can-be-obtained-by-rotation/) |
| 12 | + |
| 13 | +<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> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | + |
| 17 | +<p><strong>示例 1:</strong></p> |
| 18 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/05/20/grid3.png" style="width: 301px; height: 121px;" /> |
| 19 | +<pre> |
| 20 | +<strong>输入:</strong>mat = [[0,1],[1,0]], target = [[1,0],[0,1]] |
| 21 | +<strong>输出:</strong>true |
| 22 | +<strong>解释:</strong>顺时针轮转 90 度一次可以使 mat 和 target 一致。 |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong>示例 2:</strong></p> |
| 26 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/05/20/grid4.png" style="width: 301px; height: 121px;" /> |
| 27 | +<pre> |
| 28 | +<strong>输入:</strong>mat = [[0,1],[1,1]], target = [[1,0],[0,1]] |
| 29 | +<strong>输出:</strong>false |
| 30 | +<strong>解释:</strong>无法通过轮转矩阵中的元素使 equal 与 target 一致。 |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong>示例 3:</strong></p> |
| 34 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/05/26/grid4.png" style="width: 661px; height: 184px;" /> |
| 35 | +<pre> |
| 36 | +<strong>输入:</strong>mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]] |
| 37 | +<strong>输出:</strong>true |
| 38 | +<strong>解释:</strong>顺时针轮转 90 度两次可以使 mat 和 target 一致。 |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | + |
| 43 | +<p><strong>提示:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>n == mat.length == target.length</code></li> |
| 47 | + <li><code>n == mat[i].length == target[i].length</code></li> |
| 48 | + <li><code>1 <= n <= 10</code></li> |
| 49 | + <li><code>mat[i][j]</code> 和 <code>target[i][j]</code> 不是 <code>0</code> 就是 <code>1</code></li> |
| 50 | +</ul> |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +## 解题方法:模拟 |
| 55 | + |
| 56 | +如何将矩阵顺时针旋转90度? |
| 57 | + |
| 58 | +> 使用一个新矩阵,令新矩阵的`(i, j)`等于旧矩阵的`(j, n-i-1)`就好了。 |
| 59 | +
|
| 60 | +旋转$3$次判断$4$次,本题结束。 |
| 61 | + |
| 62 | ++ 时间复杂度$O(n^2)$ |
| 63 | ++ 空间复杂度$O(n^2)$ |
| 64 | + |
| 65 | +当然也有原地旋转矩阵的办法,如[48. 旋转图像](https://leetcode.cn/problems/rotate-image/)。 |
| 66 | + |
| 67 | +### AC代码 |
| 68 | + |
| 69 | +#### C++ |
| 70 | + |
| 71 | +```cpp |
| 72 | +/* |
| 73 | + * @LastEditTime: 2026-03-22 23:45:34 |
| 74 | + */ |
| 75 | +class Solution { |
| 76 | +public: |
| 77 | + bool findRotation(vector<vector<int>>& mat, vector<vector<int>>& target) { |
| 78 | + int n = mat.size(); |
| 79 | + for (int t = 0; t < 4; t++) { |
| 80 | + if (mat == target) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + if (t == 4) { |
| 84 | + break; |
| 85 | + } |
| 86 | + vector<vector<int>> tmp(n, vector<int>(n)); |
| 87 | + for (int i = 0; i < n; i++) { |
| 88 | + for (int j = 0; j < n; j++) { |
| 89 | + tmp[i][j] = mat[j][n - i - 1]; |
| 90 | + } |
| 91 | + } |
| 92 | + mat.swap(tmp); |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +``` |
| 99 | +
|
| 100 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/159357841)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/03/22/LeetCode%201886.%E5%88%A4%E6%96%AD%E7%9F%A9%E9%98%B5%E7%BB%8F%E8%BD%AE%E8%BD%AC%E5%90%8E%E6%98%AF%E5%90%A6%E4%B8%80%E8%87%B4/)哦~ |
| 101 | +> |
| 102 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments