-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiralMatrix.cpp
116 lines (103 loc) · 2.59 KB
/
spiralMatrix.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<iostream>
#include<vector>
using namespace std;
/*
Was asked in an actual interview,and the below solution is exactly as i coded in the interview.
It has been assumed that input matrix is given to you
constraints (row>=1&&row<=100) and (column>=1&&column<=100)
Interview was over after 40 minutes.
and the approach i explained in that interview as :-
do four traversal in one iteration
left -> right
top -> bottom
right -> left
bottom -> top
after every traversal adjust the bounds.
left -> right (top = top +1)
top -> bottom (right = right - 1)
right -> left (bottom = bottom - 1)
bottom -> top (left = left + 1)
and stop the iteration whenever any of the bounds fails to verify.
*/
class Solution
{
void horizontalTraversal(const vector<vector<int>>& inputMatrix, int left, int right, int top, vector<int>& ans)
{
for (int i = left; i <= right; i++)
{
ans.push_back(inputMatrix[top][i]);
}
}
void inverthorizontalTraversal(const vector<vector<int>>& inputMatrix, int right, int left, int bottom, vector<int>& ans)
{
for (int i = right; i >= left; i--)
{
ans.push_back(inputMatrix[bottom][i]);
}
}
void verticalTraversal(const vector<vector<int>>& inputMatrix, int top, int bottom, int right, vector<int>& ans)
{
for (int i = top; i <= bottom; i++)
{
ans.push_back(inputMatrix[i][right]);
}
}
void invertverticalTraversal(const vector<vector<int>>& inputMatrix, int bottom, int top, int left, vector<int>& ans)
{
for (int i = bottom; i >= top; i--)
{
ans.push_back(inputMatrix[i][left]);
}
}
public:
void printAns(vector<int>& arr)
{
for(auto it:arr)
{
cout << it << " ";
}
cout << endl;
}
vector<int> spiralCopy( vector<vector<int>>& inputMatrix )
{
int n = inputMatrix.size();
int m = inputMatrix[0].size();
int left = 0;
int right = inputMatrix[0].size() - 1;
int top = 0;
int bottom = inputMatrix.size() - 1;
vector<int> ans;
while (left <= right && top <= bottom)
{
if (top >= n || right < 0 || left >= m)
{
continue;
}
horizontalTraversal(inputMatrix, left, right, top, ans);
top = top + 1;
if (right < 0 || top >= n || bottom < 0)
{
continue;
}
verticalTraversal(inputMatrix, top, bottom, right, ans);
right = right - 1;
if (bottom < 0 || right < 0 || left >= m)
{
continue;
}
inverthorizontalTraversal(inputMatrix, right, left, bottom, ans);
bottom = bottom - 1;
if (left >= m || bottom < 0 || top >= n)
{
continue;
}
invertverticalTraversal(inputMatrix, bottom, top, left, ans);
left = left + 1;
}
return ans;
//return
}
};
int main()
{
}