LeetCode Pascal's Triangle
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相关推荐
- 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 ...
- 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 ...
- LeetCode——Pascal#39;s Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- LeetCode - Easy - 119. Pascal‘s Triangle II
Topic Array Description https://leetcode.com/problems/pascals-triangle-ii/ Given an integer rowIndex ...
- LeetCode - Easy - 118. Pascal‘s Triangle
Topic Array Description https://leetcode.com/problems/pascals-triangle/ Given a non-negative integer ...
- [LeetCode]119.Pascal's Triangle II
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/43562603 题目 Given an ind ...
- leetcode python3 简单题119. Pascal's Triangle II
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十九题 (1)题目 英文: Given a non-negative ind ...
- leetcode python3 简单题118. Pascal's Triangle
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十八题 (1)题目 英文: Given a non-negative int ...
- 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 ...
最新文章
- Android数据适配器Adapter简介
- gulp编译less简单demo
- Codeblocks配置EGE图形库
- Angular 如何自定义 pipe 管道以及参数传递问题
- java12/6作业1
- python离线_python 离线一个网站
- 2021-09-15频繁项集的评估标准
- MySQL和Navicat for MySQL下载及安装详细教程
- mysql c盘 越来越大_c盘分区太大怎么调整
- 新浪云SAE共享型数据库MySQL使用经验
- 《私募股权基金投资基础知识》---第二章
- 你对人工智能有什么看法?未来趋势与产品化将会如何发展?
- 插入排序、希尔排序(Shell)、选择排序、堆排序、冒泡排序、快速排序、归并排序和基数排序(桶排)的 时间复杂度和空间复杂度
- 教你怎么不添加付款方式订阅苹果arcade
- 工作多年,我对架构的一些理解
- Mybatis-plus 报错:Invalid bound statement(not found):XXX
- 开源商城系统-热猫商城店铺优惠和店铺智能排序、收益订单等功能更新
- 「超强」ChatGPT撰写的软件定制开发行业可行性报告分析
- 吲哚菁绿ICG标记海藻酸钠|ICG-海藻酸钠|alginate-Indocyaninegreen
- 现成的黄油馅饼基地行业调研报告 - 市场现状分析与发展前景预测
热门文章
- 较复杂js的书写格式
- python常用命令汇总-python数据分析之pandas常用命令整理
- python经典好书-有哪些 Python 经典书籍?
- python详细安装教程3.7.0-python 3.7.0 安装配置方法图文教程
- python输入完怎么运行-教你如何编写、保存与运行Python程序的方法
- python网络爬虫教程-教你从零开始学会写 Python 爬虫
- python怎么导入文件-Python文件如何引入?详解引入Python文件步骤
- python资料库-python 资源库
- python 类-Python 变量类型
- 廖雪峰python教程百度云-廖雪峰Python教程的配套视频教程,全套完整版!