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 ArrayList<Integer> getRow(int rowIndex) { ArrayList<Integer> res = new ArrayList<Integer>(); if(rowIndex < 0) return res; int[] arr = new int[rowIndex+1]; for(int i = 0; i <= rowIndex; i++){ for(int j = i; j >= 0; j--){ if( j == 0 || j == i){ arr[j] = 1; }else{ arr[j] = arr[j-1]+arr[j]; } } } for(int i = 0; i< arr.length; i++){ res.add(arr[i]); } return res; } }