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 ...
随机推荐
- 在前端页面中使用Markdown并且优化a标签
近期在自己的项目中加入了对 Markdown 语法 的支持,主要用到的是markedjs这个项目.该项目托管在github上,地址为:https://github.com/markedjs/marke ...
- mysql 多表关联查询
多个表右链接查询 名字,学校名称,学校类型,城市名称,国家地区 左链接查询 子查询 索引 #创建MySQL时添加索引 mysql> create table userIndex( id int ...
- Python | 迭代器与zip的一些细节
首先抛出一个困扰本人许久的问题: nums = [1,2,3,4,5,6] numsIter = iter(nums) for _ in zip(*[numsIter]*3): print(_) pr ...
- Linux 内存泄漏 valgrind
Valgrind 是个开源的工具,功能很多.例如检查内存泄漏工具---memcheck. Valgrind 安装: 去官网下载: http://valgrind.org/downloads/curre ...
- Mybatis逆向工程简单介绍
转自:https://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sq ...
- 位运算符在JS中的妙用
正文 位运算 JavaScript 中最臭名昭著的 Bug 就是 0.1 + 0.2 !== 0.3,因为精度的问题,导致所有的浮点运算都是不安全的,具体原因可详见<0.1 + 0.2不等于0. ...
- nodejs-Cluster模块
JavaScript 标准参考教程(alpha) 草稿二:Node.js Cluster模块 GitHub TOP Cluster模块 来自<JavaScript 标准参考教程(alpha)&g ...
- OC-代理,字符串
总结 编号 标题 内容 一 protocol protocol 基本概念/语法格式/protocol和继承区别/使用注意/基协议/@required和@optional关键字/类型限制 二 代理设计模 ...
- 删除数据库时报错 ERROR 1010 (HY000): Error dropping database (can't rmdir './cart', errno: 39)
这是因为在数据目录下有表相关的数据(不是表),此时应该进入存放表的目录下删除与表相关的数据,一般数据存放目录默认为/var/lib/mysql,cd到目录下 执行命令:cd /var/lib/mysq ...
- html标签设置contenteditable时,去除粘贴文本自带样式
在一个div标签里面加了可编辑的属性,从别的地方复制了一串文本,只想把文本内容存到接口里面,结果发现文本自带的标签和样式都会存进去. $(".session-new-name"). ...