LeetCode 119. Pascal's Triangle II(帕斯卡三角)

原题网址:https://leetcode.com/problems/pascals-triangle-ii/

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?


方法:

public class Solution {
    public List getRow(int rowIndex) {
        int[] rows = new int[rowIndex+1];
        int[] buf = new int[rowIndex+1];
        for(int i=0; i<=rowIndex; i++) {
            for(int j=0; j<=i; j++) {
                if (j==0 || j==i) buf[j] = 1;
                else buf[j] = rows[j-1] + rows[j];
            }
            int[] temp = rows;
            rows = buf;
            buf = temp;
        }
        List result = new ArrayList<>(rowIndex+1);
        for(int i=0; i


你可能感兴趣的:(求和,递推,三角)