题解 | #螺旋矩阵#

螺旋矩阵

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

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int> > &matrix) {
        vector<int> result;
        int ranks=matrix.size();
        if (ranks == 0)
            return {};
        int columns=matrix[0].size();
        int left=0,right=columns-1,up=0,down=ranks-1;
        int count=0;
        while (true)
        {
            for(int i=left;i<=right;i++)
            {
                result.push_back(matrix[up][i]);
                count++;
                if (count == ranks*columns)
                    return result;
            }
            up++;

            for (int i = up; i <=down ; i++)
            {
                result.push_back(matrix[i][right]);
                count++;
                if (count == ranks*columns)
                    return result;
            }
            right--;

            for (int i = right; i >= left ; i--)
            {
                result.push_back(matrix[down][i]);
                count++;
                if (count == ranks*columns)
                    return result;
            }
            down--;

            for (int i = down; i >= up ; i--)
            {
                result.push_back(matrix[i][left]);
                count++;
                if (count == ranks*columns)
                    return result;
            }
            left++;
        }
    }
};
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
05-26 09:07
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
05-24 14:12
门头沟学院 Java
点赞 评论 收藏
分享
仁者伍敌:服务员还要脱颖而出,这是五星级酒店吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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