题解 | #螺旋矩阵#

螺旋矩阵

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

模拟法
class Solution {
public:
    vector<int> spiralOrder(vector<vector<int> > &matrix)
    {
        int row = matrix.size();
        if(row == 0) return {};
        int col = matrix[0].size();
        if(col == 0) return {};
        int top = 0, bottom = row-1, left = 0, right = col-1;
        int i = 0,j = 0;
        vector<int> ret;
        while(left <= right && top <= bottom)
        {
            j = left;
            while(left <= right && top <= bottom && j <= right)
                ret.push_back(matrix[top][j++]);
            top++;
            i = top;
            while(left <= right && top <= bottom && i <= bottom)
                ret.push_back(matrix[i++][right]);
            right--;
            j = right;
            while(left <= right && top <= bottom && j >= left)
                ret.push_back(matrix[bottom][j--]);
            bottom--;
            i = bottom;
            while(left <= right && top <= bottom && i >= top)
                ret.push_back(matrix[i--][left]);
            left++;
        }
        return ret;
    }
};


全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 18:05
哈哈哈哈哈感觉朋友找工作的已经疯掉了,直接上图
码农索隆:真老板娘:“我嘞个去,这不我当年的套路吗
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务