Skip to content

Commit 6c4c600

Browse files
committed
59
1 parent 7a1e5a7 commit 6c4c600

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

59.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
4+
vector<vector<int>> generateMatrix(int n) {
5+
vector<vector<int>> matrix;
6+
matrix.resize(n, std::vector<int>(n, -1));
7+
8+
int top = 0;
9+
int bottom = n-1;
10+
int left = 0;
11+
int right = n-1;
12+
int val = 1;
13+
while (left<=right) {
14+
15+
for (int i = left;i<=right;i++) {
16+
matrix[left][i] = val++;
17+
}
18+
top++;
19+
20+
for (int i = top;i<=bottom;i++) {
21+
matrix[i][right] = val++;
22+
}
23+
right--;
24+
for (int i = right;i>=left;i--) {
25+
matrix[bottom][i] = val++;
26+
}
27+
bottom--;
28+
for (int i = bottom;i>=top;i--) {
29+
matrix[i][left] = val++;
30+
}
31+
left++;
32+
}
33+
return matrix;
34+
}
35+
};

0 commit comments

Comments
 (0)