Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

119 杨辉三角 II #30

Closed
sailei1 opened this issue May 21, 2019 · 0 comments
Closed

119 杨辉三角 II #30

sailei1 opened this issue May 21, 2019 · 0 comments

Comments

@sailei1
Copy link
Owner

sailei1 commented May 21, 2019

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

image

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

示例:

输入: 3
输出: [1,3,3,1]

/**
 * @param {number} rowIndex
 * @return {number[]}
 */
var getRow = function(rowIndex) {
    let res = []
    for (let i = 0; i <=rowIndex; i++) {
        if (i === 0) {
            res.push([1])// 第一行 基础行
        } else {
            let arrs = [1];// 初始化当前行的第一个元素为1
            let preRows = res[i - 1];// 上一行数据
            for (let j = 0; j < preRows.length; j++) {// 上一行遍历获取左上方和右上方的数的和
                arrs.push(preRows[j] + (preRows[j + 1] || 0));
            }
            res.push(arrs);
        }
    }
    return res[rowIndex];
    
};

@sailei1 sailei1 closed this as completed May 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant