119. Pascal's Triangle II(js)

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();
};

 

posted @ 2019-05-07 22:05  mingL  阅读(96)  评论(0编辑  收藏  举报