119. Pascal's Triangle II(js)

时间:2019-05-07 22:24:19   收藏:0   阅读:150

119. Pascal‘s Triangle II

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal‘s triangle.

Note that the row index starts from 0.

技术分享图片
In Pascal‘s triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 3
Output: [1,3,3,1]
题意:二维数组表示的杨辉三角,求出给定行的数据
代码如下:
/**
 * @param {number} rowIndex
 * @return {number[]}
 */
var getRow = function(rowIndex) {
        
           let res=[];
           let temp=2;        
        for(let i=0;i<=rowIndex;i++){
            res[i]=[]
            if(i>0) res[i].push(1)
            for(let j=1;j<i;j++){
                res[i].push(res[i-1][j]+res[i-1][j-1]);                
            }
            res[i].push(1);
        }         
        return res.pop();
};

 

原文:https://www.cnblogs.com/xingguozhiming/p/10828489.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!