-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path59.txt
69 lines (41 loc) · 1.5 KB
/
59.txt
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
61
62
63
64
65
66
67
68
69
class Solution {
public:
vector<vector<int>> generateMatrix(int n)
{
vector<vector<int>> m(n, vector<int>(n));
if(n<1) return m;
int begin = 0;
int rowend = n-1;
int colend = n-1;
int val =1;
while (begin <= rowend && begin <= colend )
{
for(int j = begin ;j <= colend; j++)
{
m[begin][j] = val++;
}
for (int i = begin+1; i <= rowend; i++ )
{
m[i][colend] = val ++;
}
if(begin < rowend) //遍历最后一行
{
for (int j = colend-1 ;j >=begin ; j--)
{
m[rowend][j] = val ++;
}
}
if(begin < colend) //遍历最左列
{
for (int i = rowend-1 ;i >=begin+1 ; i--)
{
m[i][begin] = val ++;
}
}
begin++;
rowend--;
colend--;
}
return m;
}
};