mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
20 lines
368 B
Go
20 lines
368 B
Go
package leetcode
|
|
|
|
func shiftGrid(grid [][]int, k int) [][]int {
|
|
x, y := len(grid[0]), len(grid)
|
|
newGrid := make([][]int, y)
|
|
for i := 0; i < y; i++ {
|
|
newGrid[i] = make([]int, x)
|
|
}
|
|
for i := 0; i < y; i++ {
|
|
for j := 0; j < x; j++ {
|
|
ny := (k / x) + i
|
|
if (j + (k % x)) >= x {
|
|
ny++
|
|
}
|
|
newGrid[ny%y][(j+(k%x))%x] = grid[i][j]
|
|
}
|
|
}
|
|
return newGrid
|
|
}
|