class Solution {
public:
vector spiralOrder(vector > &matrix) {
vector res;
int rownum = matrix.size(), colnum = matrix.size() matrix[0].size() : 0, i, j;
if (rownum == 0 || colnum == 0)
return res;
int rowstart = 0, rowend = rownum - 1, colstart = 0, colend = colnum - 1;
while (rowstart <= rowend && colstart <= colend) {
for (i = colstart; i <= colend; ++i)
res.push_back(matrix[rowstart][i]);
for (i = rowstart + 1; i <= rowend; ++i)
res.push_back(matrix[i][colend]);
if (rowstart != rowend)
for (i = colend - 1; i >
= colstart; --i)
res.push_back(matrix[rowend][i]);
if (colstart != colend)
for (i = rowend - 1; i > rowstart; --i)
res.push_back(matrix[i][colstart]);
++rowstart;
++colstart;
--rowend;
--colend;
}
return res;
}
};