/
_0118_PascalSTriangle.java
67 lines (63 loc) · 1.88 KB
/
_0118_PascalSTriangle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.diguage.algorithm.leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* = 118. Pascal's Triangle
*
* https://leetcode.com/problems/pascals-triangle/[Pascal's Triangle - LeetCode]
*
* Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
*
* In Pascal's triangle, each number is the sum of the two numbers directly above it.
*
* .Example:
* [source]
* ----
* Input: 5
* Output:
* [
* [1],
* [1,1],
* [1,2,1],
* [1,3,3,1],
* [1,4,6,4,1]
* ]
* ----
*
* @author D瓜哥, https://www.diguage.com/
* @since 2019-10-25 01:04
*/
public class _0118_PascalSTriangle {
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Pascal's Triangle.
*
* Memory Usage: 34 MB, less than 7.23% of Java online submissions for Pascal's Triangle.
*/
public List<List<Integer>> generate(int numRows) {
if (numRows < 1) {
return Collections.emptyList();
}
List<List<Integer>> result = new ArrayList<>(numRows);
result.add(Arrays.asList(1));
for (int i = 1; i < numRows; i++) {
List<Integer> parent = result.get(i - 1);
int length = parent.size() + 1;
List<Integer> current = new ArrayList<>(length);
current.add(1);
for (int j = 1; j < length; j++) {
int left = parent.get(j - 1);
int right = j >= parent.size() ? 0 : parent.get(j);
current.add(left + right);
}
result.add(current);
}
return result;
}
public static void main(String[] args) {
_0118_PascalSTriangle solution = new _0118_PascalSTriangle();
List<List<Integer>> r1 = solution.generate(5);
System.out.println(Arrays.deepToString(r1.toArray()));
}
}