leetcode 59:螺旋矩阵 II

与leetcode 54类似https://mp.csdn.net/postedit/84454025

std::vector<std::vector<int>> generateMatrix(int n){
    int top=0;
    int right=n-1;
    int left=0;
    int bottom=n-1;
    std::vector<std::vector<int>> c;
    std::vector<int> v;
    for(int i=0;i<n;i++){
        v.clear();
        for(int i=0;i<n;i++){
            v.push_back(0);
        }
        c.push_back(v);
    }
    int i=0;
    int d=0;
    while(top<=bottom&&left<=right){
        if(d%4==0){
            for(int j=left;j<=right;j++) {
                i++;
                c[top][j]=i;

            }
            top++;
        }
        else if(d%4==1){
            for(int j=top;j<=bottom;j++){
                i++;
                c[j][right]=i;

            }
            right--;
        }
        else if(d%4==2){
            for(int j=right;j>=left;j--){
                i++;
                c[bottom][j]=i;
            }
            bottom--;
        }
        else if(d%4==3){
            for(int j=bottom;j>=top;j--){
                i++;
                c[j][left]=i;
            }
            left++;
        }
        d++;
    }
    return c;
}

猜你喜欢

转载自blog.csdn.net/u013263891/article/details/84454766