给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例:

输入: 5

输出:

[

[1],

[1,1],

[1,2,1],

[1,3,3,1],

[1,4,6,4,1]

]

/**
* @param {number} numRows
* @return {number[][]}
*/
var generate = function (numRows) {
let result = [];
for (let i = 1; i <= numRows; i++) {
let tempArr = new Array(i).fill(1);
if (i > 2) {
for (let j = 1; j < tempArr.length - 1; j++) {
tempArr[j] = result[result.length - 1][j - 1] + result[result.length - 1][j];
}
}
result.push(tempArr);
}
return result;
};

leetcode 杨辉三角的更多相关文章

  1. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  2. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  3. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. LeetCode 118. 杨辉三角

    118. 杨辉三角 给定一个非负整数numRows,生成杨辉三角的前numRows行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 5 输出: [ [1], [1,1], [1,2 ...

  5. LeetCode:杨辉三角【118】

    LeetCode:杨辉三角[118] 题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: ...

  6. [LeetCode] 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 t ...

  7. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  8. 【LeetCode】119. 杨辉三角 II Pascal‘s Triangle II(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ...

  9. LeetCode Pascal's Triangle II (杨辉三角)

    题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 0ms的版本: class Solution { p ...

随机推荐

  1. Python property() 函数

    Python property() 函数  Python 内置函数 描述 property() 函数的作用是在新式类中返回属性值. 语法 以下是 property() 方法的语法: class pro ...

  2. 52. N-Queens II (Array; Back-Track)

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  3. Codeforces Beta Round #9 (Div. 2 Only)

    Codeforces Beta Round #9 (Div. 2 Only) http://codeforces.com/contest/9 A gcd水题 #include<bits/stdc ...

  4. Minimum Cost(最小费用最大流,好题)

    Minimum Cost http://poj.org/problem?id=2516 Time Limit: 4000MS   Memory Limit: 65536K Total Submissi ...

  5. python连接redis,redis集群

    python连接redis: import redis r = redis.Redis(host='192.168.50.181',port=6002) r.set('user_phone_14900 ...

  6. [leetcode]632. Smallest Range最小范围

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  7. 利用redis完成自动补全搜索功能(二)

    前面介绍了自动完成的大致思路,现在把搜索次数的功能也结合上去.我采用的是hash表来做的,当然也可以在生成分词的时候,另外一个有序集合来维护排序, 然后2个有序集合取交集即可.这里介绍hash的方式来 ...

  8. java里的静态成员变量是放在了堆内存还是栈内存

    转自http://bbs.csdn.NET/topics/370001490 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令) 2.jvm ...

  9. ecplice中代码使用快捷键无法格式化,使用其他方法将代码格式化的步骤

    选中需要进行格式化的代码--->右键--->source--->format,就可以将代码格式化了.

  10. mvc下ajax请求遇到session超时简单处理方式

    转自:http://blog.csdn.net/yeyicsdn/article/details/50032787 参考网址:http://www.cnblogs.com/RachelChen/p/5 ...