-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral_Matrix_II
More file actions
39 lines (33 loc) · 1018 Bytes
/
Spiral_Matrix_II
File metadata and controls
39 lines (33 loc) · 1018 Bytes
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
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int>> result;
for (int i=0;i<n;i++){
vector<int> row(n,0);
result.push_back(row);
}
int count=0;
for (int i=0;i<n/2;i++){
for (int j=i;j<n-i-1;j++){
count++;
result[i][j] = count;
}
for (int j=i;j<n-i-1;j++){
count++;
result[j][n-i-1] = count;
}
for (int j=i;j<n-i-1;j++){
count++;
result[n-i-1][n-j-1] = count;
}
for (int j=i;j<n-i-1;j++){
count++;
result[n-j-1][i] = count;
}
}
if (n%2==1) result[n/2][n/2] = count+1;
return result;
}
};