注意:在每次进行循环进行读写时,一定要判断left、right、top、bom的关系
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> vecResult;
if (matrix.size() == 0)return vecResult;
int rowCount = matrix.size();
int colCount = matrix[0].size();
int left = 0, right = colCount - 1;
int top = 0, bom = rowCount - 1;
while (left <= right && top <= bom)
{
for (int i = left; i <= right; i++)
{
vecResult.push_back(matrix[top][i]);
}
if(top<bom)
for (int i = top + 1; i <= bom; i++)
{
vecResult.push_back(matrix[i][right]);
}
if(top<bom&&left<right)
for (int i = right - 1; i >= left; i--)
{
vecResult.push_back(matrix[bom][i]);
}
if(top+1<bom&&left<right)
for (int i = bom - 1; i >= top+1; i--)
{
vecResult.push_back(matrix[i][left]);
}
left++; right--; top++; bom--;
}
return vecResult;
}