Skip to content

Commit bb5ea06

Browse files
committed
Time: 1 ms (54.80%) | Memory: 18.2 MB (66.74%) - LeetSync
1 parent 1e827f9 commit bb5ea06

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

1386-shift-2d-grid/shift-2d-grid.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
4+
int n = grid.size();
5+
int m = grid[0].size();
6+
int total = n * m;
7+
k = k%total;
8+
vector<vector<int>>res(n,vector<int>(m));
9+
for(int i = 0;i<n;i++){
10+
for(int j = 0;j<m;j++){
11+
int p = (i*m+j+k)%total;
12+
int newRow = p/m;
13+
int newCol = p%m;
14+
res[newRow][newCol] = grid[i][j];
15+
}
16+
}
17+
return res;
18+
}
19+
};

0 commit comments

Comments
 (0)