这题与59题相似 只不过是反着来的, 唯一不同的就是59题是 n阶矩阵 而本题是m x n 所以还会多出一个边界判断
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
int num = n * m;
vector<int> res;
int l = 0, r = n - 1, t = 0, b = m - 1;
while (num--)
{
for (int j = l; j <= r; ++j)
res.push_back(matrix[t][j]);
if(++t > b)
break;
for (int i = t; i <= b; ++i)
res.push_back(matrix[i][r]);
if(--r < l)
break;
for (int j = r; j >= l; --j)
res.push_back(matrix[b][j]);
if(--b < t)
break;
for (int i = b; i >= t; --i)
res.push_back(matrix[i][l]);
if(++l > r)
break;
}
return res;
}
};