[LeetCode] Flip Square Submatrix Vertically

3643. Flip Square Submatrix Vertically

You are given an m x n integer matrix grid, and three integers x, y, and k.

The integers x and y represent the row and column indices of the top-left corner of a square submatrix and the integer k represents the size (side length) of the square submatrix.

Your task is to flip the submatrix by reversing the order of its rows vertically.

Return the updated matrix.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
vector<vector<int>> reverseSubmatrix(vector<vector<int>>& grid, int x, int y, int k) {
for(int i = x, j = x + k - 1; i < j; i++,j--) {
for(int pos = y; pos < y + k; pos++) {
swap(grid[i][pos], grid[j][pos]);
}
}
return grid;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/08/17/PS/LeetCode/flip-square-submatrix-vertically/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.