LeetCode(33)-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].
思路:
- 要求是返回帕斯卡某一行的数据
- 设置两个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;
}
}
LeetCode(33)-Pascal's Triangle II的更多相关文章
- 【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] 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 t ...
- 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】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
- 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 ...
- C#解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 ...
随机推荐
- ORA-12519: TNS:no appropriate service handler found 解决
select count(*) from v$process --当前的连接数select value from v$parameter where name = 'processes' --数据库允 ...
- Tomcat集群如何同步会话
Tocmat集群中最重要的交换信息就是会话消息,对某个tomcat实例某会话做的更改要同步到集群其他tomcat实例的该会话对象,这样才能保证集群所有实例的会话数据一致.在tribes组件的基础上完成 ...
- iOS动画进阶 - 教你写 Slack 的 Loading 动画
(转载自:http://blog.csdn.net/wang631106979/article/details/52473985) 如果移动端访问不佳,可以访问我的个人博客 前几天看了一篇关于动画的博 ...
- java 单元测试教程(junit)
单元测试概念:最小化测试 比如说你想测试某个类中的一个方法 优点:无须启动整个程序 clipse使用junit教程: (一)配置jar: 1.右键工程选择Build Path 在二级菜单选择 Add ...
- 今天我成为了CSDN博客专家
刚刚收到了来自CSDN的回复,正如我的期待: 我随即回到自己的博客页面,欣喜地看到"专家"勋章被点亮了.Oh, yeah~ Q:我为什么要申请这个"专家"称号? ...
- java设计模式---构建者模式
创建者模式和工厂模式有点类似,不过关注点不同.工厂模式往往只关心你要的是什么,二不关心这个东西的具体细节是什么.而创建模式则关心的是这个东西的具体细节的创建.拿创建人物来说,我们关心的不仅是创建一个人 ...
- Cocos2D中图片加-hd后缀的说明
你可能注意到实际上游戏中的sprite都有2张图片,它都对应该精灵,并包含在资源包中(resource pack): player.png(27x40 pixels)和player-hd.png(do ...
- 海量数据挖掘MMDS week6: 决策树Decision Trees
http://blog.csdn.net/pipisorry/article/details/49445465 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- String之常量池小结
1.String 常量池 String使用private final char value[ ]实现字符串的存储,也就是说String创建对象之后不能够再次修改此对象中存储的字符串内容,因而Strin ...
- 分布式进阶(二)Ubuntu 14.04下安装Dockr图文教程(一)
当前,完全硬件虚拟化技术(KVM.Xen.Hyper-V 等)能在一个物理主机上很好地运行多个互相独立的操作系统,但这也带来一些问题:性能不佳,资源浪费,系统反应迟缓等.有时候对用户来说,完全的硬件虚 ...