"118-pascal ' s Triangle (Pascal triangle (Yang Hui triangle))""leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"Original Question
Given NumRows, generate the first numrows of Pascal ' s triangle.
For example, given numrows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]
Main Topic
Given a positive integer n, the n-layer Pascal triangle is obtained.
Thinking of solving problems
For any n>0, there are
F (1, N) =1, (n>0)
F (1, 2) =1, (n=2)
F (i,j) = f (i-1, j-1) +f (i, j-1), i>2,j>2
Code Implementation
Algorithm implementation class
PublicClass Solution { Public List<List<Integer>>Generate (int numrows) {if(NumRows< 0) {return NULL; }List<List<Integer>> List = NewArrayList<>();if(NumRows>= 1) {List<Integer> Data = NewArrayList<>();Data.Add1);List.AddData); }if(NumRows>= 2) {List<Integer> Data = NewArrayList<>();Data.Add1);Data.Add1);List.AddData); }if(NumRows>=3) {for (int i= 3; I<=NumRows; I++) {List<Integer> Data = NewArrayList<>();List<Integer>Prev= List.Get (i- 2);Data.Add1); for (Int J= 2; J<=I-1; J++) {Data.Add (prev.Get (J- 2)+Prev.Get (J- 1)); }Data.Add1);List.AddData); } }return List; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special InstructionsWelcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47588929"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "118-pascal ' s Triangle (Pascal Triangle)"