# LeetCode 54. Spiral Matrix

## 代码

class Solution {public:    map<int, int>mp;    vector<int> spiralOrder(vector<vector<int>>& matrix) {        vector<int>ans;        size_t m = matrix.size();        if(m == 0){            return ans;        }        size_t n = matrix[0].size();        int mi = min(n, m);        for(int i = 0 ;i < mi / 2; i++){            for(int j = i; j < n - i; j++){                ans.push_back(matrix[i][j]);                int loc = i * m + j;                mp[loc]++;            }            for(int j = i + 1; j < m - i; j++){                ans.push_back(matrix[j][n - i - 1]);                int loc = j * m + n - i - 1;                mp[loc]++;            }            for(int j = n - i - 2; j >= i; j--){                ans.push_back(matrix[m - i - 1][j]);                int loc = (m - i - 1) * m + j;                mp[loc]++;            }            for(int j = m - i - 2; j > i; j--){                ans.push_back(matrix[j][i]);                int loc = j * m + i;                mp[loc]++;            }        }        if(mi % 2){            int loc = mi / 2 * m + mi / 2;            if(mp[loc] == 0){                ans.push_back(matrix[mi / 2][mi / 2]);                mp[loc]++;            }        }        if(m == mi){            for(int i = 0; i < n - m; i++){                int loc = mi / 2 * m + mi / 2 + i + 1;                if(mp[loc] == 0){                    ans.push_back(matrix[mi / 2][mi / 2 + i + 1]);                    mp[loc]++;                }            }        }        if(n == mi){            for(int i = 0; i < m - n; i++){                int loc = (mi / 2 + i + 1) * m + mi / 2;                if(mp[loc] == 0){                    ans.push_back(matrix[mi / 2 + i + 1][mi / 2]);                    mp[loc]++;                }            }        }        return ans;    }};

0 0