LeetCode-119-杨辉三角 II

LeetCode-119-杨辉三角 II_第1张图片

class Solution {
     
	//按照公式求解即可cur[j] = pre[j-1] + pre[j];
	public static List<Integer> getRow(int rowIndex) {
     
		List<Integer> result = new LinkedList<>();    
        if (rowIndex < 2) {
     
            for (int i = 0; i <= rowIndex; i++) {
     
                result.add(1);
            }
        } else {
     
            int[] cur = new int[rowIndex+1];
            int[] pre = new int[rowIndex+1];
            pre[0] = 1;
            pre[1] = 1;
            for (int i = 1; i < rowIndex; i++) {
     
                cur[0] = 1;
                for (int j = 1; j <= i; j++) {
     
                    cur[j] = pre[j-1] + pre[j];
                }
                cur[i+1] = 1;
                pre = Arrays.copyOf(cur, i+2);
            }
            for (int i = 0; i <= rowIndex; i++) {
     
                result.add(cur[i]);
            }
        }
        return result;
	}
}

你可能感兴趣的:(算法,算法,leetcode)