题解 | #螺旋矩阵#

螺旋矩阵

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;
    }
};


全部评论

相关推荐

10-25 22:20
门头沟学院 Java
代码飞升:同学院本,个人亮点去了,打招呼里面的废话也去了,学院本就是路边一条,明天拉满然后该学还是学,小厂也行尽量先有一段实习。另外你的项目描述写的不好,具体列一下可被提问的点,然后量化一下指标或者收益吧
投了多少份简历才上岸
点赞 评论 收藏
分享
10-14 21:00
门头沟学院 Java
吃花椒的狸猫:这个人说的倒是实话,特别是小公司,一个实习生哪里来的那么多要求
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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