-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode第59题.cpp
More file actions
36 lines (36 loc) · 830 Bytes
/
Leetcode第59题.cpp
File metadata and controls
36 lines (36 loc) · 830 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
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
int loop=n/2;
int offset=1;
int startx=0;
int starty=0;
int temp=1;
int i,j;
vector<vector<int>> ans(n,vector(n,0));
while(loop--)
{
i=starty;
j=startx;
for(j=startx;j<n-offset;++j){
ans[i][j]=temp++;
}
for(i=starty;i<n-offset;++i){
ans[i][j]=temp++;
}
for(;j>offset-1;--j){
ans[i][j]=temp++;
}
for(;i>offset-1;--i){
ans[i][j]=temp++;
}
offset++;
startx++;
starty++;
}
if(n%2){
ans[n/2][n/2]=temp;
}
return ans;
}
};