Chinaunix首页 | 论坛 | 博客
  • 博客访问: 262651
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-03-22 17:00:36

import java.util.ArrayList;
import java.util.List;


//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?
public class PascalTriangleTwo {


public static void main(String[] args) {
// TODO 自动生成的方法存根


}
 public List getRow(int rowIndex) {
      
if(rowIndex<0){
return null;
}
ArrayList subtemp=new ArrayList();
ArrayList pretemp;

subtemp.add(1);
if(rowIndex==0)
   return subtemp;
pretemp=subtemp;
for(int i=1;i<=rowIndex;i++){
subtemp=new ArrayList();
subtemp.add(1);

for(int j=1;j subtemp.add(pretemp.get(j-1)+pretemp.get(j));
}
subtemp.add(1);

pretemp=subtemp;
}
return subtemp;

   }
}

阅读(245) | 评论(0) | 转发(0) |
0

上一篇:PascalsTriangle

下一篇:Pow

给主人留下些什么吧!~~