阅读设置
界面、侧栏和正文分别调整,自动保存。
主题切换
界面、侧栏和正文分别调整,自动保存。
问题描述:
给你一个 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] 解释: 上图展示了矩阵在变换前后的样子。
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;
}
}