[LeetCode][JavaScript]Pascal's Triangle

Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
https://leetcode.com/problems/pascals-triangle/
 
 
 
 

 
 
杨辉三角。
每行第一个和最后一个是1,其余是pre[i - 1] + pre[i]。
 1 /**
 2  * @param {number} numRows
 3  * @return {number[][]}
 4  */
 5 var generate = function(numRows) {
 6     var res = [], count = 0, row;
 7     while(numRows--){
 8         row = [1];
 9         if(count !== 0){
10             pre = res[count - 1];
11             for(i = 1; i < count; i++){
12                 row.push(pre[i - 1] + pre[i]);
13             }
14             row.push(1);
15         }
16         res.push(row);
17         count++;
18     }
19     return res;
20 };
 
原文地址:https://www.cnblogs.com/Liok3187/p/4842622.html