# [LeetCode]#1260. Shift 2D Grid

• Element at `grid[i][j]` moves to `grid[i][j + 1]`.
• Element at `grid[i][n - 1]` moves to `grid[i + 1][0]`.
• Element at `grid[m - 1][n - 1]` moves to `grid[0][0]`.
`Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1Output: [[9,1,2],[3,4,5],[6,7,8]]`
`Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]`
`Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9Output: [[1,2,3],[4,5,6],[7,8,9]]`
1. Covert to matrix to list.
2. Calculate how to slice based on input k.
3. Combine slice result.
4. Convert list to matrix based on moving result.
`class Solution:    def shiftGrid(self, grid, k):        col=len(grid[0])        nums=sum(grid,[])                k = k % len(nums)        move=len(nums)-k        ans=grid        count=0        nums = nums[move:] + nums[:move]        for i in range(0,len(nums),col):            ans[count]=nums[i:i+col]            count+=1        return ans`

--

--

--

## More from Fatboy Slim

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

## Fatboy Slim

Interesting in any computer science.