[leetcode]Spiral Matrix
class Solution { public: vector<int> spiralOrder(vector<vector<int> > &matrix) { // Start typing your C/C++ solution below // DO NOT write int main() function if(matrix.empty()) return vector<int>(); int M = matrix.size(); int N = matrix[0].size(); vector<int> result; int start = 0; int S = min(M, N); for(start = 0; start < S/2; start++){ for(int j = start; j < N-start; j++){ result.push_back(matrix[start][j]); } for(int i = start+1; i < M-start-1; i++){ result.push_back(matrix[i][N-1-start]); } for(int j = N-1-start; j >= start; j--){ result.push_back(matrix[M-1-start][j]); } for(int i = M-2-start; i>start; i--){ result.push_back(matrix[i][start]); } } if(M <= N && M % 2 != 0){ for(int j = start; j < N-start; j++){ result.push_back(matrix[start][j]); } } if(N < M && N % 2 != 0){ for(int i = start; i < M-start; i++){ result.push_back(matrix[i][start]); } } return result; } };