1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| class Solution:
def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:
n = len(grid)
m = len(grid[0])
# Initialize ans list
ans = []
for _ in range(n):
row = [0] * m
ans.append(row)
for i in range(n):
for j in range(m):
newJ = (j+k) % m
newI = (i+(j+k)//m) % n
ans[newI][newJ] = grid[i][j]
return ans
|