We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7a1e5a7 commit 6c4c600Copy full SHA for 6c4c600
59.cpp
@@ -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