-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpirally_traversing_a_matrix.cpp
82 lines (71 loc) · 2.06 KB
/
Spirally_traversing_a_matrix.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
/*
Problem Statement:
-----------------
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
---------
Input:
r = 4 , c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Example 2:
---------
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
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:
1 <= r , c <= 100
0 <= matrix(i) <= 100
*/
// Link --> https://practice.geeksforgeeks.org/problems/spirally-traversing-a-matrix-1587115621/1#
// Code:
class Solution
{
public:
//Function to return a list of integers denoting spiral traversal of matrix.
vector <int> spirallyTraverse(vector <vector <int>> matrix , int r , int c)
{
vector <int> result;
int i=0 , k=0 , l=0;
while(k<r && l<c)
{
//row is fixed(first) and column is increasing
for(i=l ; i<c ; i++)
result.push_back(matrix[k][i]);
k++;
//row is increasing and the column is fixed(last)
for(i=k ; i<r ; i++)
result.push_back(matrix[i][c-1]);
c--;
if(k < r)
{
//last row is fixed and column is decreasing
for(i=c-1 ; i>=l ; i--)
result.push_back(matrix[r-1][i]);
r--;
}
if(l < c)
{
//first column is fixed and row is decreasing
for(i=r-1 ; i>=k ; i--)
result.push_back(matrix[i][l]);
l++;
}
}
return result;
}
};
//comments are as per the first iterations