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));
}
}

最新文章

  1. Python Locust对指定网站“一键压测”
  2. centos6.4安装VMwareTools
  3. Kruskal算法java版
  4. flex 生命周期 ibm引用
  5. linux下php多版本的并存实现
  6. Spring Boot 入门
  7. jsonp跨域+ashx
  8. hdu 1210 Eddy&#39;s 洗牌问题
  9. Chapter 14_4 使用_ENV
  10. 用 config drive 配置网络 - 每天5分钟玩转 OpenStack(173)
  11. Linux:如何进行c++编程
  12. 关系型数据库 VS 非关系型数据库
  13. DDS生成正弦波
  14. JDK+JAVA+maven+IDEA
  15. centos7改中文
  16. C++高性能转换大小写算法
  17. yii中缓存(cache)详解
  18. 【译】SSH隧道:本地和远程端口转发
  19. [Python 网络编程] TCP编程/群聊服务端 (二)
  20. XHTML学习要点

热门文章

  1. windows 下的Python虚拟环境(vitrualen)pycharm创建Django项目
  2. python 连接ORacle11g
  3. 在dubbo工程中,使用druid监控
  4. openpyxl代码案例
  5. 【Dart学习】--Dart之数字(num)相关方法总结
  6. Linux系统调优——内核相关参数(五)
  7. spring 加载属性(properties)文件
  8. C#DbHelperOleDb,Access数据库帮助类 (转)
  9. SpringBoot中日志配置
  10. kafka删除topic后再创建同名的topic报错(ERROR org.apache.kafka.common.errors.TopicExistsException)