您的位置:首页 > 其它

【LeetCode】-Pascal's Triangle II

2014-09-22 17:08 316 查看
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 Solution {

public List<Integer> createNext( List<Integer> pre){
List<Integer> list = new ArrayList<Integer>();
list.add(1);
if( pre!=null && pre.size()>0 ){
int len = pre.size();
if( len>1 ){
for( int i=0; i<len-1; i++ ){
list.add( pre.get(i)+pre.get(i+1) );
}
}
list.add(1);
}
return list;
}

public List<Integer> getRow(int rowIndex) {
List<Integer> current = null;
for( int i=0; i<=rowIndex; i++ ){
current = createNext(current);
}
return current;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: