Skip to content

Commit 4f066cf

Browse files
committed
update: 添加问题“3643.垂直翻转子矩阵”的代码和题解 (#1456)
3643: AC.cpp (#1455) - AC,23.97%,12.77% Signed-off-by: Tisfy <Tisfy@foxmail.com>
1 parent 05c2f93 commit 4f066cf

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

.commitmsg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3643: AC.cpp (#1455) - AC,23.97%,12.77%
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-21 14:05:45
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-21 14:07:46
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
vector<vector<int>> &reverseSubmatrix(vector<vector<int>>& grid, int x, int y, int k) {
14+
for (int i = 0; i < k / 2; i++) {
15+
for (int j = 0; j < k; j++) {
16+
swap(grid[x + i][y + j], grid[x + k - i - 1][y + j]);
17+
}
18+
}
19+
return grid;
20+
}
21+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@
11301130
|3606.优惠券校验器|简单|<a href="https://leetcode.cn/problems/coupon-code-validator/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/13/LeetCode%203606.%E4%BC%98%E6%83%A0%E5%88%B8%E6%A0%A1%E9%AA%8C%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155893803" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/coupon-code-validator/solutions/3856383/letmefly-3606you-hui-quan-xiao-yan-qi-fe-vqir/" target="_blank">LeetCode题解</a>|
11311131
|3634.使数组平衡的最少移除数目|中等|<a href="https://leetcode.cn/problems/minimum-removals-to-balance-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/06/LeetCode%203634.%E4%BD%BF%E6%95%B0%E7%BB%84%E5%B9%B3%E8%A1%A1%E7%9A%84%E6%9C%80%E5%B0%91%E7%A7%BB%E9%99%A4%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157811143" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-removals-to-balance-array/solutions/3897779/letmefly-3634shi-shu-zu-ping-heng-de-zui-xpds/" target="_blank">LeetCode题解</a>|
11321132
|3637.三段式数组I|简单|<a href="https://leetcode.cn/problems/trionic-array-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/04/LeetCode%203637.%E4%B8%89%E6%AE%B5%E5%BC%8F%E6%95%B0%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157738457" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/trionic-array-i/solutions/3896318/letmefly-3637san-duan-shi-shu-zu-iyi-ci-oqj8a/" target="_blank">LeetCode题解</a>|
1133+
|3643.垂直翻转子矩阵|简单|<a href="https://leetcode.cn/problems/flip-square-submatrix-vertically/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/159318064" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/flip-square-submatrix-vertically/solutions/3932544/letmefly-3643chui-zhi-fan-zhuan-zi-ju-zh-22hn/" target="_blank">LeetCode题解</a>|
11331134
|3650.边反转的最小路径总成本|中等|<a href="https://leetcode.cn/problems/minimum-cost-path-with-edge-reversals/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/27/LeetCode%203650.%E8%BE%B9%E5%8F%8D%E8%BD%AC%E7%9A%84%E6%9C%80%E5%B0%8F%E8%B7%AF%E5%BE%84%E6%80%BB%E6%88%90%E6%9C%AC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157442073" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-cost-path-with-edge-reversals/solutions/3890549/letmefly-3650bian-fan-zhuan-de-zui-xiao-ubsdb/" target="_blank">LeetCode题解</a>|
11341135
|3651.带传送的最小路径成本|困难|<a href="https://leetcode.cn/problems/minimum-cost-path-with-teleportations/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/28/LeetCode%203651.%E5%B8%A6%E4%BC%A0%E9%80%81%E7%9A%84%E6%9C%80%E5%B0%8F%E8%B7%AF%E5%BE%84%E6%88%90%E6%9C%AC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157480540" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-cost-path-with-teleportations/solutions/3891246/letmefly-3651dai-chuan-song-de-zui-xiao-z79qr/" target="_blank">LeetCode题解</a>|
11351136
|3652.按策略买卖股票的最佳时机|中等|<a href="https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-using-strategy/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/18/LeetCode%203652.%E6%8C%89%E7%AD%96%E7%95%A5%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156061117" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-using-strategy/solutions/3860245/letmefly-3652an-ce-lue-mai-mai-gu-piao-d-cnx2/" target="_blank">LeetCode题解</a>|
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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> 表示一个&nbsp;<strong>正方形子矩阵&nbsp;</strong>的左上角下标,整数 <code>k</code> 表示该正方形子矩阵的边长。</p>
16+
17+
<p>你的任务是垂直翻转子矩阵的行顺序。</p>
18+
19+
<p>返回更新后的矩阵。</p>
20+
21+
<p>&nbsp;</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>&nbsp;</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 &lt;= m, n &lt;= 50</code></li>
55+
<li><code>1 &lt;= grid[i][j] &lt;= 100</code></li>
56+
<li><code>0 &lt;= x &lt; m</code></li>
57+
<li><code>0 &lt;= y &lt; n</code></li>
58+
<li><code>1 &lt;= k &lt;= 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

Comments
 (0)