【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
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;
}
}
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;
}
}
相关文章推荐
- LeetCode-119:Pascal's Triangle II (返回杨辉三角指定行)
- leetcode--Pascal's Triangle II
- [leetcode, python] Pascal's Triangle II 杨辉三角
- 【Leetcode】Pascal's Triangle II (Pascal)
- Leetcode Pascal's Triangle II
- leetcode||Pascal's Triangle II
- Leetcode 119. Pascal's Triangle II
- [Leetcode]119. Pascal's Triangle II
- leetcode: Pascal's Triangle II
- leetcode 119. Pascal's Triangle II
- [Leetcode] Pascal's Triangle II
- Leetcode 119. Pascal's Triangle II
- LeetCode119—Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- [Leetcode] 119. Pascal's Triangle II 解题报告
- LeetCode 119. Pascal's Triangle II
- [leetcode, python] Pascal's Triangle II 杨辉三角
- 【LeetCode-119】 Pascal's Triangle II(C++)
- 86_leetcode_Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II