白菜刷LeetCode记-118. Pascal's Triangle

今天的题目比较有趣,是构建Pascal's Triangle, 题目如下:

代码如下:

/**
 * @param {number} numRows
 * @return {number[][]}
 */
var generate = function(numRows) {
    
    let res = new Array();
  
    if(numRows == 0){
        return res;
    }
    
    res.push([1]);
    
    if(numRows == 1){
        return res;
    }
    
    for(let i = 1 ; i < numRows ; i++){
        let tmp = new Array();
        for(let j = 0 ; j < i + 1 ; j++){
            if(j == 0 || j == i){
                tmp.push(1);
            }else{
                tmp.push(res[i-1][j-1] + res[i-1][j]);
            }
        }
        res.push(tmp);
    }
    
    return res;
};
原文地址:https://www.cnblogs.com/sssysukww/p/9722091.html