-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCProblem54.cpp
More file actions
76 lines (65 loc) · 2.35 KB
/
LCProblem54.cpp
File metadata and controls
76 lines (65 loc) · 2.35 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Given an m x n matrix, return all elements of the matrix in spiral order.
// Passed - Beats 39% of submissions
// Medium Difficulty
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int colStart, colEnd, rowStart, rowEnd, i, j;
vector<int> rv;
// Get the start and end of the matrix
colStart = 0;
rowStart = 0;
rowEnd = matrix.size();
colEnd = matrix[0].size();
while( colStart < colEnd && rowStart < rowEnd ) {
i = rowStart;
j = colStart;
// iterate over the current row,
// push back everthing and set the value -101
while( j < colEnd && matrix[i][j] != -101) {
rv.push_back(matrix[i][j]);
matrix[i][j] = -101;
j++;
}
// set j to the final column in the matrix
// set i to the second row in the matrix (we've already pushed [firstRow][finalCol]
j = colEnd - 1;
i = rowStart + 1;
// iterate over the current colummn,
// push back everthing and set the value -101
while( i < rowEnd && matrix[i][j] != -101) {
rv.push_back(matrix[i][j]);
matrix[i][j] = -101;
i++;
}
// set i to the final row
// set j to the final col -1 (we've already pushed [rowEnd][colEnd]
i = rowEnd - 1;
j = colEnd - 2;
// iterate over the current row,
// push back everthing and set the value -101
while( j >= colStart && matrix[i][j] != -101) {
rv.push_back(matrix[i][j]);
matrix[i][j] = -101;
j--;
}
// set j to the first column
// set i to the final row - 1 (we've already pushed [rowEnd][colStart]
j = colStart;
i = rowEnd - 2;
// iterate over the current colummn,
// push back everthing and set the value -101
while( i > rowStart && matrix[i][j] != -101) {
rv.push_back(matrix[i][j]);
matrix[i][j] = -101;
i--;
}
// increase the starts, and decrease the ends
rowStart++;
rowEnd--;
colStart++;
colEnd--;
}
return rv;
}
};