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?
Yang Hui Triangle, Western called Pascal Triangle
Yang Hui Triangle1, each line of the number of symmetrical symmetry, starting from 1 gradually become larger, and then become smaller, back to 1. 2, the number of the nth row is n.
3, the nth row number and is 2^ (n-1).
4, each number equals the sum of the left and right two digits of the previous line. This property can be used to write out the entire Pascal Triangle.
5. The 1th digit of line 2n+1, followed by the 3rd number in line 2n+2, 5th of the 2n+3 line ... In the first line, these numbers are the 2n Fibonacci numbers. The 2nd digit of line 2n, followed by the 4th digit of line 2n+1, the 6th number of 2n+2 line ... The sum of these numbers is the 2n-1 Fibonacci number.
6, the 1th number of the nth row is 1, the second number is 1x (n-1), the third number is 1x (n-1) x (n-2)/2, fourth number is 1x (n-1) x (n-2)/2x (n-3)/3 ... And so on
7. The coefficients of the two unknowns and the N-order operations are the nth rows of the Yang Hui triangle in sequence.
To treat each row as a matrix or a vector, the nth row is one more element than the n-1 line, and the first element of each row equals 1, the last element equals the last element of the previous row, and the middle element equals the previous row's corresponding subscript and the same subscript element.
C language version:
int*getrow (intRowIndex) { int*A; A=(int*) malloc (sizeof(int) * (rowindex+1)); a[0]=1; for(inti =1; I <= RowIndex; i++) for(intj = i; J >=0; j--) if(J = =i) a[j]= a[j-1]; Else if(J = =0) A[j]=A[j]; ElseA[j]= a[j-1] +A[j]; returnA;}
C + + version:
classSolution { Public: Vector<int> GetRow (intRowIndex) {Vector<int> A (RowIndex +1); a[0] =1; for(inti =1; I <= RowIndex; i++) for(intj = i; J >=0; j--) if(J = =i) a[j]= a[j-1]; Else if(J = =0) A[j]=A[j]; ElseA[j]= a[j-1] +A[j]; returnA; }};
Pascal ' s Triangle II (Pascal Triangle)