-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangleII.java
More file actions
46 lines (33 loc) · 1.04 KB
/
PascalTriangleII.java
File metadata and controls
46 lines (33 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
40
41
42
43
44
45
46
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class PascalTriangleII {
static BigInteger factorial(int n) {
BigInteger res = new BigInteger("1");
int i;
for (i = 2; i <= n; i++)
res = res.multiply(BigInteger.valueOf(i));
return res;
}
static long ncr(int n, int r) {
BigInteger factorialN = factorial(n);
BigInteger factorialR = factorial(r);
BigInteger factorialNR = factorial(n - r);
return factorialN.divide(factorialR.multiply(factorialNR)).intValue();
}
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<>();
if (rowIndex == 0){
res.add(1);
return res;
}
for (int i = 0; i <= rowIndex; i++) {
res.add((int)ncr(rowIndex, i));
}
return res;
}
public static void main(String[] args) {
PascalTriangleII pt = new PascalTriangleII();
System.out.println(pt.getRow(3));
}
}