水题
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int i=0,j=0;
vector<int> ans;
int s=matrix.size();
if(s==0)
return ans;
int e=matrix[0].size();
if(e==0)
return ans;
int posi=1;
int posj=0;
while(true)
{
while(j<e){
ans.push_back(matrix[i][j]);
j++;
}
j--;
int tag=0;
i++;
while(i<s){
ans.push_back(matrix[i][j]);
i++;
tag=1;
}
i--;
j--;
while(j>=posj&&tag>=1)
{
ans.push_back(matrix[i][j]);
j--;
tag=2;
}
j++;
i--;
while(i>=posi&&tag>=2){
ans.push_back(matrix[i][j]);
i--;
}
i++;
//i++;
j++;
posi++;
posj++;
s--;
e--;
if(s==i||e==j)
break;
}
return ans;
}
};
原文:https://www.cnblogs.com/dacc123/p/11438043.html