题意:一个二维矩阵的值走之字形之后的值。
题解:模拟,很恶心。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ans;
int n = matrix.size();
if(n == 0) return ans;
int m = matrix[0].size();
int x1 = 0,y1 = 0,x2 = n - 1,y2 = m - 1;
while(x1 <= x2 && y1 <= y2)
{
for(int i = y1; i <= y2; i++) ans.push_back(matrix[x1][i]);
for(int i = x1 + 1; i <= x2; i++) ans.push_back(matrix[i][y2]);
if(x1 != x2)
for(int i = y2 - 1; i >= y1; i--) ans.push_back(matrix[x2][i]);
if(y1 != y2)
for(int i = x2 - 1; i >= x1 + 1; i--) ans.push_back(matrix[i][y1]);
x1++,y1++;
x2--,y2--;
}
return ans;
}
};