一:题目
二:上码
class Solution {
public:vector<int> spiralOrder(vector<vector<int>>& matrix) {if(matrix.size() == 0 || matrix[0].size() == 0){return {};}int m = matrix.size();int n = matrix[0].size();int loop = min(m/2,n/2); int starty = 0;int startx = 0;int border = 1;int i,j;vector<int> ans;while (loop--) {i = startx;j = starty;for (j = starty; j < n-border; j++) ans.push_back(matrix[i][j]);for (i = startx; i < m-border; i++) ans.push_back(matrix[i][j]);for(; j >= border; j--) ans.push_back(matrix[i][j]);for(; i >= border; i--) ans.push_back(matrix[i][j]); startx++;starty++;border++;}if (m > n && n%2 != 0) {for(; startx < m-border+1; startx++) {if(matrix[startx][starty] != NULL)ans.push_back(matrix[startx][starty]); }} else if (m < n && m%2 != 0) {for(; starty < n-border+1; starty++) { if(matrix[startx][starty] != NULL) {ans.push_back(matrix[startx][starty]);}}} else if (m%2 != 0 && m == n){int mid1 = m/2;int mid2 = n/2; ans.push_back(matrix[mid1][mid2]);}return ans;}
};
记得判空啊 记得判空 否则空指针异常