We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 6aa86e4 + 59469a9 commit d0d89f7Copy full SHA for d0d89f7
python/1260-shift-2d-grid.py
@@ -0,0 +1,16 @@
1
+class Solution:
2
+ def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:
3
+ M, N = len(grid), len(grid[0])
4
+
5
+ def posToVal(r, c):
6
+ return r * N + c
7
+ def valToPos(v):
8
+ return [v // N, v % N] # r, c
9
10
+ res = [[0] * N for i in range(M)]
11
+ for r in range(M):
12
+ for c in range(N):
13
+ newVal = (posToVal(r, c) + k) % (M * N)
14
+ newR, newC = valToPos(newVal)
15
+ res[newR][newC] = grid[r][c]
16
+ return res
0 commit comments