forked from garvit-bhardwaj/Leetcode-Problems-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiral Matrix
56 lines (56 loc) · 1.8 KB
/
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
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
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ans;
int n = matrix.size(),m = matrix[0].size();
int num = min(n,m);
if(m == 1 || n == 1){
for(int i = 0;i < matrix.size();i++){
for(int j = 0;j < matrix[0].size();j++){
ans.push_back(matrix[i][j]);
}
}
return ans;
}
unordered_map<int,int> mp;
int low = 0,high = m-1,count = 0;
int low2 = 1,high2 = n-1,count2 = m-1;
int low3 = m-1,high3 = 0,count3 = n-1;
int low4 = n-1,high4 = 1,count4 = 0;
for(int i = 0;i < matrix.size();i++){
for(int j = 0;j < matrix[0].size();j++){
mp[matrix[i][j]]++;
}
}
while(num--){
for(int i = low;i <= high;i++){
if(mp[matrix[count][i]]){
ans.push_back(matrix[count][i]);
mp[matrix[count][i]]--;
}
}
for(int i = low2;i <= high2;i++){
if(mp[matrix[i][count2]])
{
ans.push_back(matrix[i][count2]);
mp[matrix[i][count2]]--;
}
}
for(int i = low3-1;i >= high3;i--){
if(mp[matrix[count3][i]]){
ans.push_back(matrix[count3][i]);
mp[matrix[count3][i]]--;
}
}
for(int i = low4-1;i >= high4;i--){
if(mp[matrix[i][count4]]){
ans.push_back(matrix[i][count4]);
mp[matrix[i][count4]]--;
}
}
low++,high--,low2++,high2--,low3--,high3++,low4--,high4++;
count++,count2--,count3--,count4++;
}
return ans;
}
};