Tcdian / keep

今天不想做,所以才去做。
MIT License
5 stars 1 forks source link

119. Pascal's Triangle II #295

Open Tcdian opened 3 years ago

Tcdian commented 3 years ago

119. Pascal's Triangle II

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。

在杨辉三角中,每个数是它左上方和右上方的数的和

Example

Input: 3
Output: [1,3,3,1]

Follow up

Tcdian commented 3 years ago

Solution

/**
 * @param {number} rowIndex
 * @return {number[]}
 */
var getRow = function(rowIndex) {
    const reuslt = new Array(rowIndex + 1);
    for (let i = 0; i <= rowIndex; i++) {
        for (let j = i; j >= 0; j--) {
            if (j === i || j === 0) {
                reuslt[j] = 1;
            } else {
                reuslt[j] = reuslt[j] + reuslt[j - 1];
            }
        }
    }
    return reuslt;
};
function getRow(rowIndex: number): number[] {
    const reuslt: number[] = new Array(rowIndex + 1);
    for (let i = 0; i <= rowIndex; i++) {
        for (let j = i; j >= 0; j--) {
            if (j === i || j === 0) {
                reuslt[j] = 1;
            } else {
                reuslt[j] = reuslt[j] + reuslt[j - 1];
            }
        }
    }
    return reuslt;
};