Skip to content

LeetCode 每日一题笔记

0. 前言

  • 日期:2025.03.21
  • 题目:3643.垂直翻转子矩阵
  • 难度:简单
  • 标签:数组 双指针 矩阵

1. 题目理解

问题描述
给你一个 m x n 的整数矩阵 grid,以及三个整数 x、y 和 k。

整数 x 和 y 表示一个 正方形子矩阵 的左上角下标,整数 k 表示该正方形子矩阵的边长。

你的任务是垂直翻转子矩阵的行顺序。

返回更新后的矩阵。

示例

输入: grid = [1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16], x = 1, y = 0, k = 3 输出: [1,2,3,4],[13,14,15,8],[9,10,11,12],[5,6,7,16] 解释: 上图展示了矩阵在变换前后的样子。

2. 解题思路

核心观察

算法步骤

3. 代码实现

java

package com.sheeta1998.lec.lc3600_lc3699.lc3643;


public class lc3600_lc3699.lc3660.Solution {
    public int[][] reverseSubmatrix(int[][] grid, int x, int y, int k) {
        int top = x;
        int bottom = x + k - 1;

        while (top < bottom) {
            for (int i = y; i < y + k; i++) {
                int temp = grid[top][i];
                grid[top][i] = grid[bottom][i];
                grid[bottom][i] = temp;
            }
            top++;
            bottom--;
        }
        return grid;
    }
}

4. 代码优化说明

5. 复杂度分析

6. 总结

Powered by VitePress 1.6.4 | 持续更新中