[Leetcode][JAVA] Pascal's Triangle I, II
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]
] 已知行数生成帕斯卡三角。实际上只要有第i层,那么就能生成第i+1层。每次新生成的层加入最终集合中即可。
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> re = new ArrayList<List<Integer>>(); for(int i=0;i<numRows;i++) {
List<Integer> temp = new ArrayList<Integer>();
for(int j=0;j<=i;j++) {
if(j==0 || j==i)
temp.add(1);
else
temp.add(re.get(i-1).get(j-1)+re.get(i-1).get(j));
}
re.add(temp);
}
return re;
}
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,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
与第一题几乎一样,只不过不需要返回整个三角而只需要返回最后一层。全程只需要维护生成层和它上一层两个ArrayList即可。
public List<Integer> getRow(int rowIndex) {
List<Integer> re = new ArrayList<Integer>();
for(int i=0;i<=rowIndex;i++) {
List<Integer> temp = new ArrayList<Integer>();
for(int j=0;j<=i;j++) {
if(j==0 || j==i)
temp.add(1);
else
temp.add(re.get(j-1) + re.get(j));
}
re = temp;
}
return re;
}
[Leetcode][JAVA] Pascal's Triangle I, II的更多相关文章
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- [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, ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- Java for LeetCode 119 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 119 Pascal's Triangle II ----- java
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- Java [Leetcode 119]Pascal's Triangle II
题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- LeetCode 119. 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, ...
随机推荐
- CSS基础篇
写的不错,收藏 http://www.cnblogs.com/suoning/p/5625582.html
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.-解决办法
运行环境:Xcode Version 7.3.1 (7D1014) 使用NSURL进行数据请求数据代码: -(NSData *)requestData{ NSURL *url = [NSURL URL ...
- [原创] 更新Ubuntu自带的python2.X版本 ImportError: No module named pip;ImportError: No module named _sqlite3
Ubuntu14.04自带的Python2版本,是2.7.6的,想更新为最新的2.7.11,操作如下: 1. 从python官网下载2.7.11的source源码包 Python-2.7.11.tgz ...
- Python自动化 【第六篇】:Python基础-面向对象
目录: 面向过程VS面向对象 面向对象编程介绍 为什么要用面向对象进行开发 面向对象的特性:封装.继承.多态 面向过程 VS 面向对象 面向过程编程(Procedural Programming) ...
- myeclipse 在mac中字体模糊问题解决方案
找到文件:/Applications/MyEclipse 2014/MyEclipse 2014.app/Contents/Profile/myeclipse.app/Contents/Info.pl ...
- 开发中model,entity和pojo的区别
Entity接近原始数据,Model接近业务对象- Entity:是专用于EF的对数据库表的操作, Model:是为页面提供数据和数据校验的,所以两者可以并存 POJO:POJO是Plain Ordi ...
- 利用should.js进行测试
nodejs 环境 , 安装should.js包 (npm install should) var should = require('should'); //正确1, 错误0 100 precent ...
- XproerIM V1,2,12,65376 发布。
客户端下载:http://yunpan.cn/QTCxKvcpC4Iet 访问密码 9141 更新说明:1.增加表情功能. 更新代码截图:
- SQL SERVER连接、合并查询
----创建测试表MyStudentInfoCREATE table MyStudentInfo( Id int not null primary key, Name varchar(16), ...
- NPOI 读写Excel
实例功能概述: 1.支持Excel2003以及2007 2.支持Excel读取到DataTable(TableToExcel) 3.支持DataTable导出到Excel(TableToExcel) ...