-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangle.java
More file actions
39 lines (36 loc) · 1.04 KB
/
PascalTriangle.java
File metadata and controls
39 lines (36 loc) · 1.04 KB
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
package com.leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by jamylu on 2018/1/6.
* leetcode118.
* 以list的形式返回杨辉三角(帕斯卡).
*/
public class PascalTriangle {
public static void main(String[] args) {
int n = 5;
System.out.println(generate(n));
}
//动态规划
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> list = new ArrayList<>();
if (numRows == 0) {
return list;
}
int dp[][] = new int[numRows + 1][numRows + 1];
//赋初值,其余为0
dp[0][1] = 1;
for (int i = 1; i <= numRows; i++) {
// 每次都是重新申请变量
List<Integer> elem = new ArrayList<>();
for (int j = 1; j <= i; j++) {
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
elem.add(dp[i][j]);
}
// System.out.println(elem);
list.add(elem);
}
return list;
}
}