【LEETCODE】34、119题,Pascal's Triangle II
2024-08-26 21:17:02
package y2019.Algorithm.array; import java.util.ArrayList;
import java.util.List; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: GetRow
* @Author: xiaof
* @Description: 119. Pascal's Triangle II
* Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
* Note that the row index starts from 0.
* <p>
* Input: 3
* Output: [1,3,3,1]
* <p>
* 这里和之前的类似,就是获取这个塔的第几行,那么我们每次字需要存上一行,就可以求出下一行数据了
* a[i,j] = a[i - 1][j-1] + a[i - 1][j]
* @Date: 2019/7/2 9:13
* @Version: 1.0
*/
public class GetRow { public List<Integer> solution(int rowIndex) { List<Integer> preRow = new ArrayList();
preRow.add(1); //第一行
List<Integer> curRow = new ArrayList();
if (rowIndex == 0) {
return preRow;
} //如果不是第一行
curRow.add(1);
for (int i = 1; i <= rowIndex; ++i) {
//从第二行开始
curRow = new ArrayList<>();
for(int j = 0; j < i + 1; ++j) {
if(j == 0 || j == i) {
curRow.add(1);
} else {
curRow.add(preRow.get(j - 1) + preRow.get(j));
}
}
preRow = curRow;
} return curRow;
} public static void main(String args[]) {
GetRow getRow = new GetRow();
System.out.println(getRow.solution(0));
}
}
最新文章
- Python Locust对指定网站“一键压测”
- centos6.4安装VMwareTools
- Kruskal算法java版
- flex 生命周期 ibm引用
- linux下php多版本的并存实现
- Spring Boot 入门
- jsonp跨域+ashx
- hdu 1210 Eddy&#39;s 洗牌问题
- Chapter 14_4 使用_ENV
- 用 config drive 配置网络 - 每天5分钟玩转 OpenStack(173)
- Linux:如何进行c++编程
- 关系型数据库 VS 非关系型数据库
- DDS生成正弦波
- JDK+JAVA+maven+IDEA
- centos7改中文
- C++高性能转换大小写算法
- yii中缓存(cache)详解
- 【译】SSH隧道:本地和远程端口转发
- [Python 网络编程] TCP编程/群聊服务端 (二)
- XHTML学习要点
热门文章
- windows 下的Python虚拟环境(vitrualen)pycharm创建Django项目
- python 连接ORacle11g
- 在dubbo工程中,使用druid监控
- openpyxl代码案例
- 【Dart学习】--Dart之数字(num)相关方法总结
- Linux系统调优——内核相关参数(五)
- spring 加载属性(properties)文件
- C#DbHelperOleDb,Access数据库帮助类 (转)
- SpringBoot中日志配置
- kafka删除topic后再创建同名的topic报错(ERROR org.apache.kafka.common.errors.TopicExistsException)