-
Notifications
You must be signed in to change notification settings - Fork 0
/
PascalsTriangle.java
44 lines (38 loc) · 1.1 KB
/
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
package arrays;
import utils.LeetCode;
import utils.Level;
import java.util.ArrayList;
import java.util.List;
@LeetCode(no=118,
title = "Pascal's Triangle",
level = Level.EASY,
url = "https://leetcode.com/problems/pascals-triangle/"
)
public class PascalsTriangle {
public static void main(String[] args) {
}
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> list= new ArrayList<>();
if(numRows==0) return list;
int [][]nums=new int[numRows][numRows];
nums[0][0]=1;
List<Integer> levels= new ArrayList<>();
levels.add(1);
list.add(new ArrayList(levels));
levels.clear();
for(int i=1;i<nums.length;i++){
for(int j=0;j<=i;j++){
if(j==0 || j==i){
nums[i][j]=1;
}
else{
nums[i][j]+=nums[i-1][j-1]+nums[i-1][j];
}
levels.add(nums[i][j]);
}
list.add(new ArrayList(levels));
levels.clear();
}
return list;
}
}