|
| 1 | +--- |
| 2 | +title: 3643.垂直翻转子矩阵:原地修改(模拟) |
| 3 | +date: 2026-03-21 14:08:58 |
| 4 | +tags: [题解, LeetCode, 简单, 数组, 双指针, 矩阵, 模拟] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +index_img: https://assets.leetcode.com/uploads/2025/07/20/gridexmdrawio.png |
| 7 | +--- |
| 8 | + |
| 9 | +# 【LetMeFly】3643.垂直翻转子矩阵:原地修改(模拟) |
| 10 | + |
| 11 | +力扣题目链接:[https://leetcode.cn/problems/flip-square-submatrix-vertically/](https://leetcode.cn/problems/flip-square-submatrix-vertically/) |
| 12 | + |
| 13 | +<p>给你一个 <code>m x n</code> 的整数矩阵 <code>grid</code>,以及三个整数 <code>x</code>、<code>y</code> 和 <code>k</code>。</p> |
| 14 | + |
| 15 | +<p>整数 <code>x</code> 和 <code>y</code> 表示一个 <strong>正方形子矩阵 </strong>的左上角下标,整数 <code>k</code> 表示该正方形子矩阵的边长。</p> |
| 16 | + |
| 17 | +<p>你的任务是垂直翻转子矩阵的行顺序。</p> |
| 18 | + |
| 19 | +<p>返回更新后的矩阵。</p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | + |
| 23 | +<p><strong class="example">示例 1:</strong></p> |
| 24 | +<img alt="" src="https://assets.leetcode.com/uploads/2025/07/20/gridexmdrawio.png" style="width: 300px; height: 116px;" /> |
| 25 | +<div class="example-block"> |
| 26 | +<p><strong>输入:</strong> <span class="example-io">grid = </span>[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]<span class="example-io">, x = 1, y = 0, k = 3</span></p> |
| 27 | + |
| 28 | +<p><strong>输出:</strong> <span class="example-io">[[1,2,3,4],[13,14,15,8],[9,10,11,12],[5,6,7,16]]</span></p> |
| 29 | + |
| 30 | +<p><strong>解释:</strong></p> |
| 31 | + |
| 32 | +<p>上图展示了矩阵在变换前后的样子。</p> |
| 33 | +</div> |
| 34 | + |
| 35 | +<p><strong class="example">示例 2:</strong></p> |
| 36 | +<img alt="" src="https://assets.leetcode.com/uploads/2025/07/20/gridexm2drawio.png" style="width: 350px; height: 68px;" /> |
| 37 | +<div class="example-block"> |
| 38 | +<p><strong>输入:</strong> <span class="example-io">grid = [[3,4,2,3],[2,3,4,2]], x = 0, y = 2, k = 2</span></p> |
| 39 | + |
| 40 | +<p><strong>输出:</strong> <span class="example-io">[[3,4,4,2],[2,3,2,3]]</span></p> |
| 41 | + |
| 42 | +<p><strong>解释:</strong></p> |
| 43 | + |
| 44 | +<p>上图展示了矩阵在变换前后的样子。</p> |
| 45 | +</div> |
| 46 | + |
| 47 | +<p> </p> |
| 48 | + |
| 49 | +<p><strong>提示:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li><code>m == grid.length</code></li> |
| 53 | + <li><code>n == grid[i].length</code></li> |
| 54 | + <li><code>1 <= m, n <= 50</code></li> |
| 55 | + <li><code>1 <= grid[i][j] <= 100</code></li> |
| 56 | + <li><code>0 <= x < m</code></li> |
| 57 | + <li><code>0 <= y < n</code></li> |
| 58 | + <li><code>1 <= k <= min(m - x, n - y)</code></li> |
| 59 | +</ul> |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +## 解题方法:原地修改 |
| 64 | + |
| 65 | +令$i$从$0$到$\lfloor\frac{k}2\rfloor$枚举,交换$x+i$行和$x+k-i-1$行的列数为从$y$到$y+k-1$的对应元素。 |
| 66 | + |
| 67 | ++ 时间复杂度$O(k^2)$ |
| 68 | ++ 空间复杂度$O(1)$ |
| 69 | + |
| 70 | +### AC代码 |
| 71 | + |
| 72 | +#### C++ |
| 73 | + |
| 74 | +```cpp |
| 75 | +/* |
| 76 | + * @LastEditTime: 2026-03-21 14:07:46 |
| 77 | + */ |
| 78 | +class Solution { |
| 79 | +public: |
| 80 | + vector<vector<int>> &reverseSubmatrix(vector<vector<int>>& grid, int x, int y, int k) { |
| 81 | + for (int i = 0; i < k / 2; i++) { |
| 82 | + for (int j = 0; j < k; j++) { |
| 83 | + swap(grid[x + i][y + j], grid[x + k - i - 1][y + j]); |
| 84 | + } |
| 85 | + } |
| 86 | + return grid; |
| 87 | + } |
| 88 | +}; |
| 89 | +``` |
| 90 | +
|
| 91 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/159318064)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/03/21/LeetCode%203643.%E5%9E%82%E7%9B%B4%E7%BF%BB%E8%BD%AC%E5%AD%90%E7%9F%A9%E9%98%B5/)哦~ |
| 92 | +> |
| 93 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments