Pascal's 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?
1 public class Solution { 2 public ArrayList<Integer> getRow(int rowIndex) { 3 ArrayList<Integer> res = new ArrayList<Integer>(); 4 int num [] = new int [rowIndex+2]; 5 num[0]=1; 6 num[1] =1; 7 8 for(int i=2;i<=rowIndex;i++){ 9 for(int j=i;j>=0;j--){ 10 if(j==i || j==0) 11 num[j] = 1; 12 else{ 13 num[j] = num[j]+num[j-1]; 14 } 15 } 16 } 17 for(int i=0;i<rowIndex+1;i++){ 18 res.add(num[i]); 19 } 20 return res; 21 } 22 }