数组-118. 杨辉三角

  • 题目描述
    • 题目样例
  • Java方法:数学
    • 思路及算法
    • 代码
    • 复杂度

题目描述

给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。
在「杨辉三角」中,每个数是它左上方和右上方的数的和。

题目样例

  • 示例1:
输入: numRows = 5
输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
  • 示例2:
输入: numRows = 1
输出: [[1]]
  • 提示:
1 <= numRows <= 30

Java方法:数学

思路及算法

杨辉三角,是二项式系数在三角形中的一种几何排列。它是中国古代数学的杰出研究成果之一,它把二项式系数图形化,把组合数内在的一些代数性质直观地从图形中体现出来,是一种离散型的数与形的结合。
杨辉三角具有以下性质:

依据性质 4,我们可以一行一行地计算杨辉三角。每当我们计算出第 i 行的值,我们就可以在线性时间复杂度内计算出第 i+1 行的值。

代码

class Solution {public List<List<Integer>> generate(int numRows) {List<List<Integer>> ret = new ArrayList<List<Integer>>();for (int i = 0; i < numRows; ++i) {List<Integer> row = new ArrayList<Integer>();for (int j = 0; j <= i; ++j) {if (j == 0 || j == i) {row.add(1);} else {row.add(ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j));}}ret.add(row);}return ret;}
}

复杂度

  • 时间复杂度:O(numRows ^2)。
  • 空间复杂度:O(1)。不考虑返回值的空间占用。
    [1]:https://leetcode-cn.com/problems/pascals-triangle/solution/yang-hui-san-jiao-by-leetcode-solution-lew9/

LeetCode刷题-8相关推荐

  1. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  2. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  3. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  4. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  5. LeetCode刷题记录11——290. Word Pattern(easy)

    LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...

  6. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  7. LeetCode刷题记录9——58. Length of Last Word(easy)

    LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...

  8. LeetCode刷题记录8——605. Can Place Flowers(easy)

    LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...

  9. LeetCode刷题记录7——824. Goat Latin(easy)

    LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...

  10. LeetCode刷题记录6——696. Count Binary Substrings(easy)

    LeetCode刷题记录6--696. Count Binary Substrings(easy) 目录 LeetCode刷题记录6--696. Count Binary Substrings(eas ...

最新文章

  1. Windows 08 R2_创建AD DS域服务(图文详解)
  2. 触发器初接触-同步两个表的指定字段
  3. Python 字符串(三)-字符串操作实例(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
  4. Python基于opencv调用摄像头获取个人图片
  5. Hadoop HDFS原理
  6. 实施ERP管理系统的流程有哪些
  7. 关于学习单片机keil uvision5的基本使用方法
  8. c++ 调用meshlab程序慢_从Meshlab学架构
  9. 网络层 详解,网络层功能,网络层协议,网络层设备。
  10. socket: (10106) 无法加载或初始化请求的服务提供程序。
  11. 《经营的本质》1-“经营的基本元素”读后感及读书笔记
  12. “开源社”成立:众人同心,其利断金
  13. Unity 画面质量设置
  14. win10安装visual studio2008
  15. 3.HTML——表格数据
  16. win32GDI函数编程实现推箱子小游戏
  17. 计算机二级c语言编译,全国计算机等级考试教程-二级C语言.pdf
  18. Java学习路线!2020-2021华为Java面试真题
  19. 美国网红python微博_Python有趣|微博网红大比拼 !
  20. 量化考核指标的TQC法

热门文章

  1. 软件测试培训适合什么人学习?
  2. leetcode--删除排序数组中的重复项--python
  3. 详解Java中的复合视图设计模式
  4. nginx tomcat https
  5. statpot:使用mongo+bootstrap+highcharts做统计报表
  6. 【HDOJ】3275 Light
  7. SAP QM 'QM System' 有什么控制作用?
  8. .Net 程序员面试 C# 语言篇 (回答Scott Hanselman的问题)
  9. vPower系列1: vMotion-没有vMotion,虚拟化只是玩具
  10. 非结构化信息-》半结构化-》结构化-》关联数据体系-》数据挖掘-》故事化呈现-》决策导向