3643. Flip Square Submatrix Vertically
/**
* Problem: LeetCode 3643 - Flip Square Submatrix Vertically
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int[][] flipSubmatrix(int[][] grid, int x, int y, int k) {
int top = x;
int bottom = x + k - 1;
while (top < bottom) {
for (int j = y; j < y + k; j++) {
int temp = grid[top][j];
grid[top][j] = grid[bottom][j];
grid[bottom][j] = temp;
}
top++;
bottom--;
}
return grid;
}
}
"""
Problem: LeetCode 3643 - Flip Square Submatrix Vertically
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
class Solution:
def flipSubmatrix(self, grid: list[list[int]], x: int, y: int, k: int) -> list[list[int]]:
top = x
bottom = x + k - 1
while top < bottom:
for j in range(y, y + k):
grid[top][j], grid[bottom][j] = grid[bottom][j], grid[top][j]
top += 1
bottom -= 1
return grid
/**
* Problem: LeetCode 3643 - Flip Square Submatrix Vertically
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> flipSubmatrix(vector<vector<int>>& grid, int x, int y, int k) {
int top = x;
int bottom = x + k - 1;
while (top < bottom) {
for (int j = y; j < y + k; ++j) {
swap(grid[top][j], grid[bottom][j]);
}
top++;
bottom--;
}
return grid;
}
};
/**
* Problem: LeetCode 3643 - Flip Square Submatrix Vertically
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
var flipSubmatrix = function(grid, x, y, k) {
let top = x;
let bottom = x + k - 1;
while (top < bottom) {
for (let j = y; j < y + k; j++) {
const temp = grid[top][j];
grid[top][j] = grid[bottom][j];
grid[bottom][j] = temp;
}
top++;
bottom--;
}
return grid;
};