#include<iostream>
#include<vector>
using namespace std;
class MyClass
{
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
if (matrix.empty()){
return res;
}
int endRow = matrix.size() - 1;
int endCol = matrix[0].size() - 1;
int startRow = 0;
int startCol = 0;
while (startCol <= endCol && startRow <= endCol){
for (int i = startCol; i <= endCol; ++i){
res.push_back(matrix[startRow][i]);
}
if (startRow < endRow){
for (int i = startRow + 1; i <= endRow; ++i){
res.push_back(matrix[i][endCol]);
}
}
if (startRow < endRow&&startCol < endCol){
for (int i = endCol - 1; i >= startCol; --i){
res.push_back(matrix[endRow][i]);
}
}
if (startRow + 1 < endRow&&startCol < endCol){
for (int i = endRow - 1; i > startRow; --i){
res.push_back(matrix[i][startCol]);
}
}
++startCol;
++startRow;
--endCol;
--endRow;
}
return res;
}
};
顺时针打印矩阵
最新推荐文章于 2024-08-19 17:00:22 发布