9просмотров
64.3%от подписчиков
21 марта 2026 г.
📷 ФотоScore: 10
#leetcode #easy 3643. Flip Square Submatrix Vertically Дейлик литкода 21.03.2026 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. class Solution { public int[][] reverseSubmatrix(int[][] grid, int x, int y, int k) { for (int i0 = x, i1 = x + k - 1; i0 < i1; i0++, i1--) { for (int j = y; j < y + k; j++) { int temp = grid[i0][j]; grid[i0][j] = grid[i1][j]; grid[i1][j] = temp; } } return grid; }
} func reverseSubmatrix(grid [][]int, x int, y int, k int) [][]int { for i0, i1 := x, x+k-1; i0 < i1; i0, i1 = i0+1, i1-1 { for j := y; j < y+k; j++ { grid[i0][j], grid[i1][j] = grid[i1][j], grid[i0][j] } } return grid
} class Solution { fun reverseSubmatrix(grid: Array<IntArray>, x: Int, y: Int, k: Int): Array<IntArray> { for (i in 0 until k / 2) { val top = x + i val bottom = x + k - 1 - i for (j in y until y + k) { grid[top][j] = grid[bottom][j].also { grid[bottom][j] = grid[top][j] } } } return grid }
}