118. Pascal's Triangle

来源:互联网 发布:ip地址及端口号 编辑:程序博客网 时间:2024/06/05 00:13

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]
]

var generate = function(n) {    var result = [];    if(n == 0) {        return result    }else if(n == 1) {        return [[1]]    }    result[0] = [1];    result[1] = [1,1];    for (var row = 2; row < n; row++){        result[row] = [1];        for (var col = 1; col <= row -1; col++){            result[row][col] = result[row-1][col] + result[row-1][col-1];            result[row].push(1);        }    }    return result;};
1 0