119. Pascal's Triangle II

Given an indexk, return thekthrow of the Pascal's triangle.

For example, givenk= 3,
Return[1,3,3,1].

思路:另一种思路,不用存上一行

public class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> res = new ArrayList<Integer>();
        if (rowIndex<0) return res;

        for (int i=0;i<rowIndex+1;i++){
            res.add(0,1);
            for (int j=1;j<res.size()-1;j++){
                res.set(j, res.get(j)+res.get(j+1));
            }
        }
        return res;
    }
}

results matching ""

    No results matching ""