[Algorithm] 118. Pascal's Triangle

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
/**
 * @param {number} numRows
 * @return {number[][]}
 */
var generate = function(numRows) {
    
    if (numRows === 0) {
        return [];
    }
    
    let result = [];
    
    for (let i = 0; i < numRows; i++) {
        let row = [];
        result.push(row);
        for (let j = 0; j < i + 1; j++) {
            if (j === 0 || j === i) {
                row.push(1);
            } else {
                const temp = result[i-1][j-1] + result[i-1][j];
                row.push(temp);
            }
        }
        result[i] = row;
    }
   
    return result;
};
原文地址:https://www.cnblogs.com/Answer1215/p/12051318.html