Given an index k, return the k th 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?
import java.util.*;
import java.lang.*;
public class Solution {
public ArrayList<Integer> getRow(int rowIndex) {
int[] last=new int[rowIndex+1];
int[] now=new int[rowIndex+1];
int[] tmp=new int[rowIndex+1];
last[0]=1;
for(int i=1;i<=rowIndex;i++){
now[0]=1;
now[i]=1;
for(int j=1;j<i;j++){
now[j]=last[j-1]+last[j];
}
tmp=last;
last=now;
now=tmp;
}
ArrayList ans=new ArrayList<Integer>();
for(int i=0;i<=rowIndex;i++){
ans.add(last[i]);
}
return ans;
}
}