[leetcode刷题系列]Pascal's Triangle II

依旧没啥好说的, 利用滚动数据就可以做到O(k)的空间了

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int a[2][rowIndex + 1];
        a[0][0] = 1;
        int last = 0;
        for(int i = 1; i <= rowIndex; ++ i){
            int now = 1 - last;
            a[now][0] = 1;
            a[now][i] = 1;
            for(int j =  1; j < i; ++ j)
                a[now][j] = a[last][j - 1] + a[last][j];
            last = now;
        }
        vector<int> vc;
        for(int i = 0; i <= rowIndex; ++ i)
            vc.push_back(a[last][i]);
        return vc;
    }
};


你可能感兴趣的:([leetcode刷题系列]Pascal's Triangle II)