-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20_SpiralMatrixII.cpp
61 lines (52 loc) · 1.46 KB
/
20_SpiralMatrixII.cpp
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
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> res0;
if(n == 0)return res0;
vector<vector<int>> res(n, vector<int>(n, 0));
int height = n, width = n;
int hindex = 0, windex = 0;
int count = 1;
while(hindex < height/2.0 && windex < width/2.0){
int i = hindex;
for(int j = windex; j < width - windex; ++j){
res[i][j] = count++;
}
int j = width - windex - 1;
for(int i = hindex + 1; i < height - hindex; ++i){
res[i][j] = count++;
}
i = height - hindex - 1;
if(i > hindex && width > 1){
for(int j = width - windex - 2; j >= windex; --j){
res[i][j] = count++;
}
}
j = windex;
if(j < width - windex - 1){
for(int i = height - hindex - 2; i >= hindex + 1; --i){
res[i][j] = count++;
}
}
++hindex;
++windex;
}
return res;
}
};
int main()
{
Solution s;
int n = 7;
vector<vector<int>> res = s.generateMatrix(n);
for(int i = 0; i < n; ++i){
for(int j = 0;j < n; ++j){
cout << res[i][j] << " ";
}
cout<< endl;
}
return 0;
}