-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path59.cpp
More file actions
23 lines (23 loc) · 720 Bytes
/
Copy path59.cpp
File metadata and controls
23 lines (23 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ret(n, vector<int>(n, 0));
int N = pow(n, 2), m = 1;
int left = 0, right = n - 1, top = 0, down = n - 1;
while (top <= down && left <= right) {
for (int y = left; y <= right; ++y)
ret[top][y] = m++;
++top;
for (int x = top; x <= down; ++x)
ret[x][right] = m++;
--right;
for (int y = right; y >= left; --y)
ret[down][y] = m++;
--down;
for (int x = down; x >= top; --x)
ret[x][left] = m++;
++left;
}
return ret;
}
};