给定一个非负整数 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. 230. Kth Smallest Element in a BST 找到bst中的第k小的元素

    [抄题]: Given a binary search tree, write a function kthSmallest to find the kth smallest element in i ...

  2. XAMPP下apache部署网站,多个虚拟机(空间)配置

     之 前在 Mac 下一直使用 MAMP 搭建本地 php 环境,因为其强大的组合配置以及方便的 UI 操作.因为公司的电脑是 Windows 的,有时候需要搭建 php 环境来调试一些小东西方便自己 ...

  3. discuz的diy功能介绍

    可以通过页面操作的方式,完成页面布局设计,数据聚合,样式等常见的页面处理功能.   以管理员登陆discuz的前台时,会出现一个diy按钮. 流程,先设计框架,再完成数据的聚合.     定义模板时, ...

  4. JsonConvert.SerializeObject 空值处理

    var settings = new JsonSerializerSettings() { ContractResolver= new NullToEmptyStringResolver() }; v ...

  5. maven的下载

    1.maven的下载地址:http://maven.apache.org/download.cgi.下载3.3.9版本(在选择下载的版本是要与JDK契合). 2.下载之后,解压的文件放的位置尽量不要有 ...

  6. Linux安装和配置Vim7.4

    一.简介 Vim是一个类似于Vi的文本编辑器,不过在Vi的基础上增加了很多新的特性,Vim普遍被推崇为类Vi编辑器中最好的一个,事实上真正的劲敌来自Emacs的不同变体.1999 年Emacs被选为L ...

  7. linux 动态库 静态库 函数覆盖

    本文讨论了linux动态库  静态库中函数的覆盖问题. 测试目的: 同名函数,分别打成动态库libdync_lib.so与静态库libstatic_lib.a,并把libstatic_lib.a打到另 ...

  8. PID算法(C语言)

    /************ PID算法(C语言) ************/ #include <stdio.h> #include<math.h> struct _pid { ...

  9. fastjson解析List对象

    List<String[]> body = JSON.parseObject(msg.getBody().toString(), new TypeToken<List<Stri ...

  10. 用eclipse创建maven的webapp项目

    来自http://www.cnblogs.com/candle806/p/3439469.html和http://blog.csdn.net/chuyuqing/article/details/288 ...