LeetCode

题目:

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?

思路:

递归

package recursion;

import java.util.List;
import java.util.ArrayList;

public class PascalsTriangleII {

    public List<Integer> getRow(int rowIndex) {
        List<Integer> res;
        if (rowIndex == 0) {
            res = new ArrayList<Integer>();
            res.add(1);
        } else {
            res = getRow(rowIndex - 1);
            for (int i = res.size() - 1; i > 0; --i) 
                res.set(i, res.get(i) + res.get(i - 1));            
            res.add(1);
        } 
        
        return res;
    }

}
原文地址:https://www.cnblogs.com/null00/p/5127493.html