找规律 : 定位好left,right,up,bottom四个边界,防止越界
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
if(matrix.size()<1)
return res;
int bot = matrix.size()-1, right = matrix[0].size()-1;
int left = 0, up = 0;
while (left<=right&&up<=bot&&right>=0&&bot>=0)
{
for (int i = left; i<=right; i++)
res.push_back(matrix[up][i]);
up++; //print from the left to the right
for (int i = up; i<=bot; i++)
res.push_back(matrix[i][right]);
right--; //print from the up to the bottom
for (int i =right; i >= left&&(up-1)!=bot; i--)
res.push_back(matrix[bot][i]);
bot--; //print from the right to the left
for (int i = bot; i >= up&&left!=(right+1); i--)
res.push_back(matrix[i][left]);
left++; //print from the bottom to the up
}
return res;
}
};
485



被折叠的 条评论
为什么被折叠?



