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

题意:给出行数,输出杨辉三角

思路:第一行输出1,第二行输出[1,1],第三行除了首尾为1外,其余的为上一行的两个相邻的和

public class Solution {public List<List<Integer>> generate(int numRows) {List<List<Integer>> ans = new ArrayList<List<Integer>>();for (int i = 0; i < numRows; i++) {ArrayList<Integer> arrList = new ArrayList<Integer>();if (i == 0) {arrList.add(1);ans.add(arrList);} else if (i == 1) {arrList.add(1);arrList.add(1);ans.add(arrList);} else {arrList.add(1);List<Integer> prev = ans.get(i - 1);for (int j = 1, len = prev.size(); j < len; j++) {int tmp = prev.get(j - 1) + prev.get(j);arrList.add(tmp);}arrList.add(1);ans.add(arrList);}      }return ans;}
}

LeetCode Pascal's Triangle相关推荐

  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 II

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

  3. LeetCode——Pascal#39;s Triangle

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

  4. LeetCode - Easy - 119. Pascal‘s Triangle II

    Topic Array Description https://leetcode.com/problems/pascals-triangle-ii/ Given an integer rowIndex ...

  5. LeetCode - Easy - 118. Pascal‘s Triangle

    Topic Array Description https://leetcode.com/problems/pascals-triangle/ Given a non-negative integer ...

  6. [LeetCode]119.Pascal's Triangle II

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/43562603 题目 Given an ind ...

  7. leetcode python3 简单题119. Pascal's Triangle II

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十九题 (1)题目 英文: Given a non-negative ind ...

  8. leetcode python3 简单题118. Pascal's Triangle

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十八题 (1)题目 英文: Given a non-negative int ...

  9. LeetCode 118 Pascal's Triangle(帕斯卡三角形)(vector)

    翻译 给定一个行数字,生成它的帕斯卡三角形.例如,给定numRows = 5, 返回: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1] ] 原文 Given numR ...

最新文章

  1. Android数据适配器Adapter简介
  2. gulp编译less简单demo
  3. Codeblocks配置EGE图形库
  4. Angular 如何自定义 pipe 管道以及参数传递问题
  5. java12/6作业1
  6. python离线_python 离线一个网站
  7. 2021-09-15频繁项集的评估标准
  8. MySQL和Navicat for MySQL下载及安装详细教程
  9. mysql c盘 越来越大_c盘分区太大怎么调整
  10. 新浪云SAE共享型数据库MySQL使用经验
  11. 《私募股权基金投资基础知识》---第二章
  12. 你对人工智能有什么看法?未来趋势与产品化将会如何发展?
  13. 插入排序、希尔排序(Shell)、选择排序、堆排序、冒泡排序、快速排序、归并排序和基数排序(桶排)的 时间复杂度和空间复杂度
  14. 教你怎么不添加付款方式订阅苹果arcade
  15. 工作多年,我对架构的一些理解
  16. Mybatis-plus 报错:Invalid bound statement(not found):XXX
  17. 开源商城系统-热猫商城店铺优惠和店铺智能排序、收益订单等功能更新
  18. 「超强」ChatGPT撰写的软件定制开发行业可行性报告分析
  19. 吲哚菁绿ICG标记海藻酸钠|ICG-海藻酸钠|alginate-Indocyaninegreen
  20. 现成的黄油馅饼基地行业调研报告 - 市场现状分析与发展前景预测

热门文章

  1. 较复杂js的书写格式
  2. python常用命令汇总-python数据分析之pandas常用命令整理
  3. python经典好书-有哪些 Python 经典书籍?
  4. python详细安装教程3.7.0-python 3.7.0 安装配置方法图文教程
  5. python输入完怎么运行-教你如何编写、保存与运行Python程序的方法
  6. python网络爬虫教程-教你从零开始学会写 Python 爬虫
  7. python怎么导入文件-Python文件如何引入?详解引入Python文件步骤
  8. python资料库-python 资源库
  9. python 类-Python 变量类型
  10. 廖雪峰python教程百度云-廖雪峰Python教程的配套视频教程,全套完整版!