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;
}
}