-
Notifications
You must be signed in to change notification settings - Fork 160
/
shift-2d-grid.md
43 lines (34 loc) · 1.62 KB
/
shift-2d-grid.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<p>Given a 2D <code>grid</code> of size <code>n</code> * <code>m</code> and an integer <code>k</code>. You need to shift the <code>grid</code> <code>k</code> times.</p>
<p>In one shift operation:</p>
<ul>
<li>Element at <code>grid[i][j]</code> becomes at <code>grid[i][j + 1]</code>.</li>
<li>Element at <code>grid[i][m - 1]</code> becomes at <code>grid[i + 1][0]</code>.</li>
<li>Element at <code>grid[n - 1][m - 1]</code> becomes at <code>grid[0][0]</code>.</li>
</ul>
<p>Return the <em>2D grid</em> after applying shift operation <code>k</code> times.</p>
<p> </p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/05/e1.png" style="width: 400px; height: 178px;" />
<pre>
<strong>Input:</strong> <code>grid</code> = [[1,2,3],[4,5,6],[7,8,9]], k = 1
<strong>Output:</strong> [[9,1,2],[3,4,5],[6,7,8]]
</pre>
<p><strong>Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/05/e2.png" style="width: 400px; height: 166px;" />
<pre>
<strong>Input:</strong> <code>grid</code> = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
<strong>Output:</strong> [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> <code>grid</code> = [[1,2,3],[4,5,6],[7,8,9]], k = 9
<strong>Output:</strong> [[1,2,3],[4,5,6],[7,8,9]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length <= 50</code></li>
<li><code>1 <= grid[i].length <= 50</code></li>
<li><code>-1000 <= grid[i][j] <= 1000</code></li>
<li><code>0 <= k <= 100</code></li>
</ul>