[leetcode]Pascal's Triangle II

递推,滚动数组

 

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<vector<int> > f(2 , vector<int>(rowIndex+1 , 0));
        int mark = 0;
        f[mark][0] = 1;f[mark][1] = 1; // 1
        mark ++ ; mark  %= 2;
        for(int i = 1 ; i < rowIndex ; i++){
            f[mark][0] = 1;
            f[mark][i + 1] = 1;
            for(int j = 1 ; j <= i ; j++){
                f[mark][j] = f[(mark+1)%2][j-1] + f[(mark+1)%2][j];
            }
            mark ++ ; mark %=2;
        }
        return f[(mark+1) %2];
    }
};

 

你可能感兴趣的:(LeetCode)