"119-pascal ' s Triangle II (Pascal triangle (Yang Hui's Triangle) II)"
" leetcode-interview algorithm classic-java Implementation" "All topic Directory Index"
Original title
Given a 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 spaces?
The main effect of the topic
Given a positive integer k, the line K of Pascal is obtained.
ideas for solving problems
For any n>0, there are
F (1, N) =1, (n>0)
F (n, N) =1, (N>2)
F (i,j) = f (i-1, j-1) +f (i, j-1), I>2,j>2,
Find the K line.
Code Implementation
Algorithm implementation class
Import java.util.*;
Number of substantive data for public class Solution {public list<integer> getRow (int rowIndex) {rowindex++;//RowIndex
if (RowIndex < 0) {return null;
} list<integer> result = new arraylist<> (rowIndex);
if (RowIndex >= 1) {result.add (1);
} if (RowIndex >= 2) {result.add (1); int line = 0; record which line of int prev is currently being used;
Which line is the previous line if (RowIndex >= 3) {int[][] data = new Int[2][rowindex];
"1" data[0][0] = 1;
Data[1][0] = 1;
DATA[1][1] = 1; for (int i = 2; i < RowIndex. i++) {line = i% 2;//new calculated data saved in No. 0 or 1th row prev = (i-1 +
2)% 2; Data[line][0] = 1; Set the first number, you can not, "1" has been done, data[x][0] total 1 for (int j = 1; j < I; J +) {Data[line][j] = Data[prev][j-1] + DATA[PREV][J]; } Data[line][i] = 1;
Set last number}//Result.clear ();
for (int i = 0; i < RowIndex i++) {result.add (data[line][i]);
} return result;
}
}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, released in a new window to view the full picture.
Special Notes Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47589235"