【leetcode】119. Yang Hui Triangle II (JS implementation)

1. Topic

119. Yang Hui Triangle II
insert image description here

2. Idea

insert image description here

3. Code implementation

/**
 * @param {number} rowIndex
 * @return {number[]}
 */
var getRow = function(rowIndex) {
    
    
    let curRow = [], preRow = []
    for (let i = 0; i <= rowIndex; i++) {
    
    
        curRow = new Array(i + 1).fill(0)
        // 每一行的首尾
        curRow[0] = curRow[i] = 1
        // 其他地方
        for (let j = 1; j < i; j++) {
    
    
            curRow[j] = preRow[j] + preRow[j - 1]
        }
        preRow = curRow
    }
    return preRow
};

4. Reference

Yang Hui Triangle II

Guess you like

Origin blog.csdn.net/weixin_44109827/article/details/129350744