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 optimize your algorithm to use only O(k) extra space?
1 Public classSolution {2 PublicList<integer> GetRow (intRowIndex) {3integer[] num =NewInteger[rowindex+1];4Num[0] = 1;5 for(inti = 1; i < num.length; i++){6Num[i] = (int)((Long) num[i-1]* (rowindex-i+1)/i);//Do not strengthen the conversion, product greater than integer.max_value have overflow7 }8 returnarrays.aslist (num);9 }Ten}
1 Public classSolution {2 PublicList<integer> GetRow (intRowIndex) {3list<integer> list =NewArraylist<integer>();4List.add (1);5 for(inti = 1; I < RowIndex + 1; i++){6List.add ((int)((Long) List.get (i-1) * (rowindex-i+1)/i));7 }8 returnlist;9 }Ten}
According to the mathematical formula:
[C(k,0), C(k,1), ..., C(k, k-1), C(k, k)]
C[k,i] = C[k,i-1]*(k-i+1)/i
119 Pascal ' s Triangle II Java Solutions