Implementation of Yang Hui's triangle leetcode Pascal ' s Triangle_java based on Java

Source: Internet
Author: User

Pascal ' s triangle

Given numrows, generate the 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]
]

This problem is relatively simple, Yang Hui's triangle, you can use this column of elements equal to its top two elements of the sum to ask.

Mathematical solid people will see, in fact, each column is a mathematical arrangement, line 4th, you can use C30 = 0 c31=3 c32=3 c33=3 to seek

Import java.util.ArrayList;
Import java.util.List;
public class Par {public
static void Main (string[] args) {
System.out.println (Generate (1));
SYSTEM.OUT.PRINTLN (Generate (0));
SYSTEM.OUT.PRINTLN (Generate (2));
SYSTEM.OUT.PRINTLN (Generate (3));
SYSTEM.OUT.PRINTLN (Generate (4));
SYSTEM.OUT.PRINTLN (Generate (5));

}
public static list<list<integer>> Generate (int numrows) {
list<list<integer>> result = New Arraylist<list<integer>> (numrows);
for (int i = 0; i < numrows i++) {
list<integer> thisrow = new arraylist<integer> (i);
Thisrow.add (1);
int temp = 1;
int row = i;
for (int j = 1; J <= I; j +) {
temp = temp * row--/J;
Thisrow.add (temp);
}
Result.add (Thisrow);
}
return result;
}

The above content introduces the Yang Hui's triangle Leetcode Pascal's triangle knowledge based on the Java, Hope everybody likes.

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.