[leetcode] PascalsTriangle2

/**
* <pre>
* 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?
* </pre>
* */
public class PascalsTriangle2 {
    public class Solution {
        public List<Integer> getRow(int rowIndex) {
            List<Integer> result = new ArrayList<Integer>();
            for (int i = 0; i <= rowIndex; i++) {
                result.add(1);
                for (int j = result.size() - 2; j >= 1; j--) {
                    result.set(j, result.get(j) + result.get(j - 1));
                }
            }
            return result;
        }
    }
}

你可能感兴趣的:(LeetCode)