题解 | #螺旋矩阵#

螺旋矩阵

http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31

public:
    vector<int> spiralOrder(vector<vector<int> > &matrix) {
        if (matrix.empty() || matrix[0].empty()) return {};
        vector<int> res;
        int up = 0;
        int down = matrix.size() - 1;
        int left = 0;
        int right = matrix[0].size() - 1;
        while(true){
            for(int i=left; i<=right; i++) res.push_back(matrix[up][i]);
            if(++up > down) break;
            for(int i=up; i<=down; i++) res.push_back(matrix[i][right]);
            if(--right < left) break;
            for(int i=right; i>=left; i--) res.push_back(matrix[down][i]);
            if(--down < up) break;
            for(int i=down; i>=up; i--) res.push_back(matrix[i][left]);
            if(++left > right) break;
        }
        return res;
        
        
    }
};
https://www.cnblogs.com/grandyang/p/4362675.html
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务