Pascal's Triangle II

Source

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?

Java

public class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> cur = new ArrayList<Integer>();
        cur.add(1);
        for(int i=1; i<=rowIndex; i++){
            List<Integer> pre = cur;
            cur = new ArrayList<Integer>();
            cur.add(1);
            for(int j=0; j<pre.size()-1; j++){
                cur.add(pre.get(j)+pre.get(j+1));
            }
            cur.add(1);
        }
        return cur;
    }
}