-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1260. Shift 2D Grid.java
38 lines (32 loc) · 1.05 KB
/
1260. Shift 2D Grid.java
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
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
while(k-- > 0)
{
int prev = grid[grid.length-1][grid[0].length-1];
for(int rowIdx = 0; rowIdx < grid.length; rowIdx++)
{
for(int colIdx = 0; colIdx < grid[rowIdx].length; colIdx++)
{
int temp = grid[rowIdx][colIdx];
grid[rowIdx][colIdx] = prev;
prev = temp;
}
}
}
return createList(grid);
}
List<List<Integer>> createList(int[][] grid)
{
List<List<Integer>> result = new ArrayList<>();
for(int rowIdx = 0; rowIdx < grid.length; rowIdx++)
{
ArrayList<Integer> smallRes = new ArrayList<>();
for(int colIdx = 0; colIdx < grid[rowIdx].length; colIdx++)
{
smallRes.add(grid[rowIdx][colIdx]);
}
result.add(smallRes);
}
return result;
}
}