Pascal's Triangle
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]
]
This question is relatively simple. In the Yang Hui triangle, you can use the elements in this column to find the sum of the two elements above its head.
Those who are solid in mathematics will see that in fact, each column is a combination of arrangements in mathematics. In the 4th row, you can use C30 = 0 C31=3 C32=3 C33=3 to find it.
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 you to the relevant knowledge of implementing the LeetCode Pascal's Triangle based on Java. I hope you like it.