LeetCode # Array # Easy #118. Pascal's Triangle 119. Pascal's Triangle II
118 题目:又叫“杨辉三角形”
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
思路:用动态规划。首先,第0行的数字为1;其次,每行的第一个和最后一个为1;最后,其他的值为他的上一行左边+上一行右边(与其接壤)的值。
具体做法:将三角形看做一个大列表,每行作为一个行列表存储在大列表中。
1 class Solution { 2 public List<List<Integer>> generate(int numRows) { 3 List<List<Integer>> triangle = new ArrayList<List<Integer>>();//存储整个三角形,其中每行也是一个整型列表。 4 if(numRows == 0){//当要求0行,则输出空的列表; 5 return triangle; 6 } 7 triangle.add(new ArrayList<>());//第一行永为1 8 triangle.get(0).add(1); 9 for(int numRow = 1; numRow < numRows; numRow ++){ 10 List<Integer> row = new ArrayList<>(); 11 List<Integer> prevrow = triangle.get(numRow-1); 12 row.add(1);//每行第一个=1 13 for(int j =1; j < numRow ; j++){ 14 row.add(prevrow.get(j-1)+prevrow.get(j));//中间的元素等于上一行左右两个的和; 15 } 16 row.add(1);//每行最后一个=1; 17 triangle.add(row); 18 } 19 return triangle; 20 } 21 }
119:在上题基础了修改,三角形行数从0开始,且输出的是第K行的列表,要求辅助空间大小为O(K);
思路:只需要保存当前行和上一行的信息,输出时输出当前行。
1 class Solution { 2 public List<Integer> getRow(int rowIndex) { 3 List<Integer> row = new ArrayList<>();//当前行 4 List<Integer> prevRow ;//上一行 5 if(rowIndex == 0){//0行只有1 6 row.add(1); 7 } 8 for(int numRow = 1; numRow <=rowIndex; numRow ++){ 9 prevRow = row; //前一行== 上一次循环后的row 10 row = new ArrayList<>();//重新计算当前行 11 row.add(1); 12 for(int j =1; j < numRow ; j++){ 13 row.add(prevRow.get(j-1)+prevRow.get(j)); 14 } 15 row.add(1); 16 } 17 return row; 18 } 19 }
转载于:https://www.cnblogs.com/DongPingAn/p/8987837.html
LeetCode # Array # Easy #118. Pascal's Triangle 119. Pascal's Triangle II相关推荐
- LeetCode # Array # Easy # 217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode - Easy - 118. Pascal‘s Triangle
Topic Array Description https://leetcode.com/problems/pascals-triangle/ Given a non-negative integer ...
- [LeetCode]119.Pascal#39;s Triangle II
题目 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [ ...
- [LeetCode]119.Pascal's Triangle II
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/43562603 题目 Given an ind ...
- LeetCode 119. Pascal’s Triangle II
119. Pascal's Triangle II My Submissions QuestionEditorial Solution Total Accepted: 72147 Total Subm ...
- C#LeetCode刷题之#118-杨辉三角(Pascal‘s Triangle)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3688 访问. 给定一个非负整数 numRows,生成杨辉三角的前 ...
- 从零开始刷Leetcode——数组(118.119.121)
文章目录 119.杨辉三角2 118.杨辉三角 121.买卖股票的最佳时机 119.杨辉三角2 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右 ...
- LeetCode简单题目(#118 #119 #121 #122 #125 #136 #141 #155)-8道
leetcode题库中共有350道简单题目. 本文记录已解决的题目和代码. 本文中的序号是leetcode题目中的真实序号. 文章目录 118 杨辉三角 描述 代码 官方解答 119 杨辉三角II 描 ...
- leetcode (Tree easy)
leetcode Tree easy # leetcode Tree easy problem class TreeNode(object):def __init__(self,val):self.v ...
- 第十三章第一节(Triangle类)(Triangle class)
第十三章第一节(Triangle类)(Triangle class) **13.1(Triangle类)设计一个继承了抽象类GeometriObject的新的Triangle类.绘制Triangle类 ...
最新文章
- 自动刷新某个指定网页
- android 不可点击状态,Android开机指引后notification应为不可点击状态
- [转]Flex unit testflex-4-create-a-simple-unit-test-in-flash-builder
- linux 获取模块,get_module - 获取Linux内核模块的详细信息
- HDU 1384 Intervals【差分约束-SPFA】
- Vue 父子组件间的通信
- 杂记 C中的volatile
- linux查看进程和终止进程
- php 类定义抽象方法吗,如何理解php的抽象类跟抽象方法
- 创建SSIS包—建立端到端的package
- 欧盟:2020年之前普及免费WiFi网络
- Linux2.6用户空间堆栈区的分配与回收
- 多元统计分析最短距离法_多元统计分析习题及解答.doc
- 布谷鸟哈希函数的参数_布谷鸟算法详细讲解
- 蓝桥杯备赛第一天-138译码器
- Android简易聊天室软件(HTTP实现)
- java bouncycastle,使用BouncyCastle在Java中使用ECIES进行加密
- 数学建模养老保险问题matlab,全国大学生数学建模竞赛C题 企业退休职工养老金制度的改革...
- C语言编程 体型预测
- Visual Studio中使用Macros插件给代码添加注释、时间和以及自动脚本
热门文章
- 8数据提供什么掩膜产品_英特尔推出多款数据中心产品:继续提供差异化选项...
- 取消ajax请求时页面闪烁,基于JQuery的$.ajax方法进行异步请求导致页面闪烁的解决办法...
- linux 终端 拼音,告诉你Ubuntu中文智能拼音输入法配置的方法及命令
- java制作大富翁游戏_JAVA大富翁游戏的设计+流程图+总结体设计图-论文.doc
- sklearn gridsearchcv_sklearn调包侠之PCA降维
- linux shell 常用命令总结
- 微信小程序云开发教程-WXSS入门-常用样式
- java List操作
- mysql+alter+int_MySQL Alter命令
- vue学习-路由router