-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode059_Spiral_Matrix_II.cpp
More file actions
55 lines (42 loc) · 1.14 KB
/
LeetCode059_Spiral_Matrix_II.cpp
File metadata and controls
55 lines (42 loc) · 1.14 KB
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
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
int x = 0;
int y = 0;
int left = 0;
int right = n;
int up = 0;
int down = n;
int count = 1;
vector<int> tmp(n, 0);
vector<vector<int>> res(n, tmp);
while(1)
{
for(int i = left; i < right; i++)
{
res[x][y++] = count++;
}
up++; x++; y--;
if(up == down)
break;
for(int i = up; i < down; i++)
{
res[x++][y] = count++;
}
right--; x--; y--;
for(int i = right-1; i >= left; i--)
{
res[x][y--] = count++;
}
down--; x--; y++;
if(up == down)
break;
for(int i = down-1; i >= up; i--)
{
res[x--][y] = count++;
}
left++; x++; y++;
}
return res;
}
};