Topic:
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?
Ideas:
Because the number of each layer is symmetric, you only need to calculate the value of the first half of each layer, and the second half copies. Res[i] = Res ' [i-1] + res ' [i] (RES ' stands for the original array, res represents the updated array), and TMP stands for res ' [I-1],TMP2 stands for res ' [i].
Code:
int* GetRow (intRowIndex,int*returnsize) { if(RowIndex <0)returnNULL; ++RowIndex; int*res = (int*)malloc(sizeof(int) *RowIndex); res[0] =1; intCount =RowIndex; while(--count) { intReal_index = Rowindex-count +1; intHalf = Real_index/2; if(Real_index%2==0) {Half--; } intTMP = res[0]; inti =1; for(; I <= half; i++){ intTMP2 =Res[i]; Res[i]= tmp +TMP2; TMP=TMP2; } if(Real_index%2) {i-=2; } Else { --i; } for(intj = Half +1; J < Real_index; J + +) {Res[j]= res[i--]; } } *returnsize =RowIndex; returnRes;}
Leetcode oj:pascal ' s Triangle II