-
Notifications
You must be signed in to change notification settings - Fork 0
/
038. spiral matrix print.cpp
63 lines (56 loc) · 1.76 KB
/
038. spiral matrix print.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
Spirally traversing a matrix - GeeksForGeeks
Given a matrix of size r*c. Traverse the matrix in spiral form.
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 spiral-pattern technique as known, output for the this testcase will be 1 2 3 4 8 12 11 10 9 5 6 7.
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)
{
// your code here
//index initialisation
int startRow = 0;
int startCol = 0;
int endRow = r - 1;
int endCol = c - 1;
//storing answer
vector<int> ans;
int count = 0;
int total = r * c;
while(count < total) {
//printing firstRow
for(int i=startCol;count<total && i<=endCol;i++) {
ans.push_back(matrix[startRow][i]);
count++;
}
startRow++;
//printing endCol
for(int i=startRow;count<total && i<=endRow;i++) {
ans.push_back(matrix[i][endCol]);
count++;
}
endCol--;
//printing endRow
for(int i=endCol;count<total && i>=startCol;i--) {
ans.push_back(matrix[endRow][i]);
count++;
}
endRow--;
//printing firstCol
for(int i=endRow;count<total && i>=startRow;i--) {
ans.push_back(matrix[i][startCol]);
count++;
}
startCol++;
}
return ans;
}
};