题解 | #螺旋矩阵#
螺旋矩阵
http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
模拟法
class Solution { public: vector<int> spiralOrder(vector<vector<int> > &matrix) { int row = matrix.size(); if(row == 0) return {}; int col = matrix[0].size(); if(col == 0) return {}; int top = 0, bottom = row-1, left = 0, right = col-1; int i = 0,j = 0; vector<int> ret; while(left <= right && top <= bottom) { j = left; while(left <= right && top <= bottom && j <= right) ret.push_back(matrix[top][j++]); top++; i = top; while(left <= right && top <= bottom && i <= bottom) ret.push_back(matrix[i++][right]); right--; j = right; while(left <= right && top <= bottom && j >= left) ret.push_back(matrix[bottom][j--]); bottom--; i = bottom; while(left <= right && top <= bottom && i >= top) ret.push_back(matrix[i--][left]); left++; } return ret; } };