-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral_Print2D_Matrix.cpp
More file actions
58 lines (50 loc) · 1.68 KB
/
Spiral_Print2D_Matrix.cpp
File metadata and controls
58 lines (50 loc) · 1.68 KB
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
/*
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, R and C as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(R*C)
Expected Auxiliary Space: O(R*C)
Constraints:
2 <= R, C <= 100
0 <= matrixi <= 100
Input:
R = 3, C = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
*/
vector<int> spirallyTraverse(vector<vector<int> > matrix, int r, int c)
{
// code here
vector<int>res;
int top = 0, left = 0, right = c-1, bottom = r-1;
int size = c*r;
while(left <= right && top<=bottom){
for(int i = left; i<=right && res.size()<size; i++){
// cout<<matrix[left][i]<<" ";
res.push_back(matrix[top][i]);
}
top++;
for(int i = top; i<=bottom && res.size()<size; i++){
// cout<<matrix[i][right]<<" ";
res.push_back(matrix[i][right]);
}
right--;
for(int i = right; i>=left && res.size()<size; i--){
// cout<<matrix[bottom][i]<<" ";
res.push_back(matrix[bottom][i]);
}
bottom--;
for(int i = bottom; i>=top && res.size()<size; i--){
// cout<<matrix[i][left]<<" ";
res.push_back(matrix[i][left]);
}
left++;
}
return res;
}