-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode1914.cpp
More file actions
52 lines (37 loc) · 1.45 KB
/
leetcode1914.cpp
File metadata and controls
52 lines (37 loc) · 1.45 KB
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
44
45
46
47
48
49
50
51
52
class Solution {
public:
vector<vector<int>> rotateGrid(vector<vector<int>>& grid, int k) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> ans = grid;
int layers = min(m, n) / 2;
for (int layer = 0; layer < layers; layer++) {
int top = layer, left = layer;
int bottom = m - 1 - layer, right = n - 1 - layer;
vector<int> vals;
for (int c = left; c <= right; c++)
vals.push_back(grid[top][c]);
for (int r = top + 1; r < bottom; r++)
vals.push_back(grid[r][right]);
for (int c = right; c >= left; c--)
vals.push_back(grid[bottom][c]);
for (int r = bottom - 1; r > top; r--)
vals.push_back(grid[r][left]);
int len = vals.size();
int shift = k % len;
vector<int> rotated(len);
for (int i = 0; i < len; i++) {
rotated[i] = vals[(i + shift) % len];
}
int idx = 0;
for (int c = left; c <= right; c++)
ans[top][c] = rotated[idx++];
for (int r = top + 1; r < bottom; r++)
ans[r][right] = rotated[idx++];
for (int c = right; c >= left; c--)
ans[bottom][c] = rotated[idx++];
for (int r = bottom - 1; r > top; r--)
ans[r][left] = rotated[idx++];
}
return ans;
}
};