119. Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3, Return [1,3,3,1].

0                       [1],
1                      [1,1],
2                     [1,2,1],
3                    [1,3,3,1],
4                   [1,4,6,4,1]
//返回特定行

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> row(rowIndex + 1);

        int prev = 1, cur = 1;
        for(int i = 0; i <= rowIndex; i++){
            //i行
            row[0] = row[i] = 1;
            //i行有i + 1个元素
            for(int j = i - 1; j > 0; j--){
                row[j] = row[j] + row[j - 1];
            }
        }
        return row;
    }
};

results matching ""

    No results matching ""