[leedcode 119] Pascal's Triangle II

2114 ワード

Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,Return  [1,3,3,1] .
public class Solution {
    public List<Integer> getRow(int rowIndex) {
        // O(k) , , , !
        List<Integer> list=new ArrayList<Integer>();
        List<Integer> list1=new ArrayList<Integer>();
        if(rowIndex<0) return list;
        for(int i=0;i<=rowIndex;i++){
            for(int j=0;j<=i;j++){
                if(j==0||j==i) list1.add(1);
                else{
                    list1.add(list.get(j)+list.get(j-1));
                }
            }
            List<Integer> temp=list;/////
            list=list1;
            list1=temp;
            list1.clear();
        }
        return list;
    }
}