"Leetcode-Interview algorithm classic-java Implementation" "118-pascal ' s Triangle (Pascal Triangle)"

Source: Internet
Author: User

"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)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.