-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question17.cpp
51 lines (43 loc) · 1.32 KB
/
Question17.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
17 -> Spiral Matrix
Solution :
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int>ans;
int n=matrix.size();
int m=matrix[0].size();
int count=0;
int total=n*m;
int startingrow=0;
int endingrow=n-1;
int startingcol=0;
int endingcol=m-1;
while(count<total){
for(int i=startingcol;i<=endingcol&&count<total;i++){
int ele=matrix[startingrow][i];
ans.push_back(ele);
count++;
}
startingrow++;
for(int i=startingrow;i<=endingrow&&count<total;i++){
int ele=matrix[i][endingcol];
ans.push_back(ele);
count++;
}
endingcol--;
for(int i=endingcol;i>=startingcol&&count<total;i--){
int ele=matrix[endingrow][i];
ans.push_back(ele);
count++;
}
endingrow--;
for(int i=endingrow;i>=startingrow&&count<total;i--){
int ele=matrix[i][startingcol];
ans.push_back(ele);
count++;
}
startingcol++;;
}
return ans;
}
};