LeetCode(33)-Pascal's Triangle II
2016-04-07 21:37
351 查看
题目:
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1].
思路:
要求是返回帕斯卡某一行的数据设置两个list变量,pre和next,pre用来存储当前的最后的最后一行,next存储下一行,next赋值给pre(在这之前pre清空,之后next清空),往下移动
-
代码:
public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> pre = new ArrayList<Integer>(); List<Integer> next = new ArrayList<Integer>(); pre.add(1); if(rowIndex == 0){ return pre; } for(int i = 1;i < rowIndex+1;i++){ next.add(1); for(int a = 1;a < i;a++){ next.add(pre.get(a-1)+pre.get(a)); } next.add(1); pre.clear(); for(int o =0;o < next.size();o++ ){ pre.add(next.get(o)); } next.clear(); } return pre; } }
相关文章推荐
- POJ1659 Frogs' Neighborhood
- Program2_1008
- jq获取鼠标位置
- 对话框
- 解决gradle project sync failed.Basic functionality(e.g.editing,debugging) will not work properly
- JAVA集合(一)
- Python抓取电影天堂电影信息的代码
- 关于C语言的问卷调查
- 【BZOJ 1016】【JSOI 2008】最小生成树计数
- SICP(二):过程抽象
- WebSocket ——多人实时聊天
- DPDK与Socket区别
- 对团队项目的意见和项目需求的分析
- 并查集初学——围巾的纠结
- 在Dynamic Web Project中新建jsp报错
- USACO3.2 还是暴力+bitset学习
- C++直接初始化与复制初始化的区别深入解析
- 图论算法之最小生成树
- DOM XSS的原理与防护
- nginx 301跳转到带www域名方法rewrite(转)