spiral order matrix traversal code example
Example: spiral order matrix traversal in C++
vector<int> Solution::spiralOrder(const vector<vector<int> > &A) { int row_start = 0, row_end = A.size()-1, col_start = 0, col_end = A[0].size()-1; vector<int> v; while (row_start<=row_end && col_start<=col_end) { //for row_start for (int col = col_start; col<=col_end; col++) { v.push_back(A[row_start][col]); } row_start++; //for col_end for (int row = row_start; row<= row_end; row++) { v.push_back(A[row][col_end]); } col_end--; //for row_end for (int col = col_end; col>=col_start; col--) { v.push_back(A[row_end][col]); } row_end--; //for col_start for (int row = row_end; row>=row_start; row--) { v.push_back(A[row][col_start]); } col_start++; } return v;}XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX