LeetCode118. Pascal's Triangle 杨辉三角
题目
给定行数,生成对应的杨辉三角
思考
- 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2
- 对于第偶数行,个数也是偶数,对于奇数行,个数也是奇数
- 而且对于非第一列的数,d[i][j]=d[i-1][j-1]+d[i-1][j]
后面通过编程实现,发现自己第一个条件是完全没有用的,可以不需要用到对称性,直接由上一行计算下一行(即第三条)。
而且忘记说最后一列了。
所以新的思考点是:
- 对于非第一列/最后一列的数,d[i][j]=d[i-1][j-1]+d[i-1][j]
- 第一列和最后一列初始化为1即可
出现过的错误
粗心
由于粗心把当前行数i用总行数numRows代替,所以出现了下标出界的情况Line 18: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0。
没想清楚
还有没想清楚关于对称点和i、j的关系,所以总是出界。最后考虑取消这个点,从总体情况考虑。
局部变量带来的问题
把lineInts声明在函数域中,结果放入总的嵌套List中出了问题。然后在for的局部域中声明这个变量,问题就解决了。
代码
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> results=new ArrayList<List<Integer>>(numRows);
for(int i=0;i<numRows;i++){//i代表当前行数-1
ArrayList<Integer> lineInts=new ArrayList<Integer>();//这一行的数字
lineInts.add(1);//每行第一个是1
int j=1;
for(;j<i;j++){
int sum=results.get(i-1).get(j-1)+results.get(i-1).get(j);
lineInts.add(sum);
}
if(i>0)
lineInts.add(1);//最后加一个1
results.add(lineInts);
}
return results;
}
总结
成绩是32%,应该还有优化的地方,回头再考虑一下吧。
LeetCode118. Pascal's Triangle 杨辉三角的更多相关文章
- [leetcode-118]Pascal's triangle 杨辉三角
Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Pascal's Triangle(杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【LeetCode每天一题】Pascal's Triangle(杨辉三角)
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- [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, ...
- 20190105-打印字母C,H,N,口等图像和杨辉三角
1. 打印字母C ****** * * * * ****** def print_c(n): print('*' * n) for i in range(n): print('* ') print(' ...
- [Swift]LeetCode118. 杨辉三角 | Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- [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 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- 前后端分离进阶一:使用ElementUI+前端分页
前两篇入门:前后端分离初体验一:前端环境搭建 前后端分离初体验二:后端环境搭建+数据交互 参考:https://www.bilibili.com/video/BV137411B7vB B站UP:楠哥教 ...
- 16. Linux find查找文件及文件夹命令
find的主要用来查找文件,查找文件的用法我们比较熟悉,也可用它来查找文件夹,用法跟查找文件类似,只要在最后面指明查找的文件类型 -type d,如果不指定type类型,会将包含查找内容的文件和文件夹 ...
- Ganglia 简单介绍与安装
文章来至于 http://sachinsharm.wordpress.com/2013/08/17/setup-and-configure-ganglia-3-6-on-centosrhel-6- ...
- c#中实现串口通信的几种方法
c#中实现串口通信的几种方法 通常,在C#中实现串口通信,我们有四种方法: 第一:通过MSCOMM控件这是最简单的,最方便的方法.可功能上很难做到控制自如,同时这个控件并不是系统本身所带,所以还得注册 ...
- clickhouse输入输出格式 TSKV CSV
TSKVTSKV格式不适合有大量小列的输出.TSKV的效率并不比JSONEachRow差.TSKV数据查询和数据导入.不需要保证列的顺序. 支持忽略某些值,这些列使用默认值,例如0和空白行.复杂类型的 ...
- MySQL(5):安装MySQL
下载地址 下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-winx64.zip 安装步骤 第一步:下载得到压缩包,并解压 ...
- SpringBoot(1):初始SpringBoot
一. SpringBoot 简介 1. SpringBoot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特 ...
- excel数据导入mySql数据库
1.将excel数据保存好 2.打开数据库,在表上点击右键,选择导入向导 3.点击下图中红色部门,点击下一步 4.选择excel文件的位置,下方的表空间内,会出现excel中的sheet页,选择要导入 ...
- 使用Spring Data ElasticSearch框架来处理索引
/**步骤:创建工程,导入相应的包--->配置文件---->创建实体类对象------>创建接口---->测试增删改查的方法 **/ //步骤:创建工程,导入相应的包 < ...
- eclipse.ini配置 vmargs 说明
-vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M 1. 各个参数的含义什么? 参数中-vmargs的意思是设置JVM参数, ...