Leetcode Note: Go - Shift 2d Grid

Shift 2D Grid - LeetCode
https://leetcode.com/problems/shift-2d-grid/

  • Go 言語で取り組んだメモ

回答

Shift 2D Grid - LeetCode
https://leetcode.com/problems/shift-2d-grid/solutions/2506983/easy-to-follow-go-solution/?orderBy=most_votes&languageTags=golang

func shiftGrid(grid [][]int, k int) [][]int {
	n := len(grid)
	m := len(grid[0])

	ans := make([][]int, n)
	for i := 0; i < len(ans); i++ {
		ans[i] = make([]int, m)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			x := (k / m) % n
			y := k % m

			ans[x][y] = grid[i][j]
			k++
		}
	}

	return ans
}