# Pascal ' s Triangle solution

Source: Internet
Author: User

Question

Given numrows, generate the first numrows of Pascal ' s triangle.

For example, given numrows = 5,
Return

`[     1], [    1,2,1], [   1,3,3,1], [  1,4,6,4,1]`
Solution

Key to the solution are to use the arrays, one for current list, and one for previous array.

`1  Public classSolution {2      PublicList<list<integer>> Generate (intnumrows) {3list<list<integer>> result =NewArraylist<list<integer>>();4         if(NumRows = = 0)5             returnresult;6List<integer> prev =NewArraylist<integer>();7Prev.add (1);8 Result.add (prev);9          while(NumRows > 1) {TenList<integer> current =NewArraylist<integer>(); One             intLength =prev.size (); ACurrent.add (1); -              for(inti = 0; i < length-1; i++) -Current.add (Prev.get (i) + prev.get (i + 1)); theCurrent.add (1); - Result.add (current); -Prev =Current ; -numrows--; +         } -         returnresult; +     } A}`

Pascal ' s Triangle solution

Related Keywords:

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.