-
Notifications
You must be signed in to change notification settings - Fork 413
/
Spiral Matrix
30 lines (30 loc) · 853 Bytes
/
Spiral Matrix
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
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int>v;
int row=matrix.size();
int col=matrix[0].size();
int c=0,total=row*col;
int sr=0,er=row-1,sc=0,ec=col-1;
while(c<total){
for(int i=sc;i<=ec && c<total;i++){
v.push_back(matrix[sr][i]);
c++;
}
sr++;
for(int i=sr;i<=er && c<total;i++){
v.push_back(matrix[i][ec]);
c++;
}
ec--;
for(int i=ec;i>=sc && c<total;i--){
v.push_back(matrix[er][i]);
c++;
}
er--;
for(int i=er;i>=sr && c<total;i--){
v.push_back(matrix[i][sc]);
c++;
}
sc++;
}
return v;
}