File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ vector<int > spiralOrder (vector<vector<int >>& matrix) {
4+ enum direction { R, D, L, U };
5+
6+ enum direction dir = R;
7+ vector<int > result;
8+ int min_R = 0 ;
9+ int max_R = matrix.size () - 1 ;
10+ int min_C = 0 ;
11+ int max_C = matrix[0 ].size () - 1 ;
12+ int r = 0 ;
13+ int c = 0 ;
14+ int size = matrix.size () * matrix[0 ].size ();
15+
16+ while (result.size () < size){
17+ result.push_back (matrix[r][c]);
18+
19+ switch (dir){
20+ case R:
21+ if (c == max_C){
22+ dir = D;
23+ min_R++;
24+ r++;
25+ }else
26+ c++;
27+ break ;
28+ case D:
29+ if (r == max_R){
30+ dir = L;
31+ max_C--;
32+ c--;
33+ }else
34+ r++;
35+ break ;
36+ case L:
37+ if (c == min_C){
38+ dir = U;
39+ max_R--;
40+ r--;
41+ }else
42+ c--;
43+ break ;
44+ case U:
45+ if (r == min_R){
46+ dir = R;
47+ min_C++;
48+ c++;
49+ }else
50+ r--;
51+ break ;
52+ }
53+ }
54+
55+ return result;
56+ }
57+ };
You can’t perform that action at this time.
0 commit comments