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.