File tree Expand file tree Collapse file tree 2 files changed +43
-11
lines changed Expand file tree Collapse file tree 2 files changed +43
-11
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .List ;
3
+
4
+
5
+ /**
6
+ *
7
+ * this is solution of Pascal's Triangle
8
+ *
9
+ * https://oj.leetcode.com/problems/pascals-triangle/
10
+ *
11
+ */
12
+ public class PascalTriangle {
13
+
14
+ public List <List <Integer >> generate (int numRows ) {
15
+ List <List <Integer >> result = new ArrayList <>();
16
+
17
+ if (numRows <= 0 ) {
18
+ return result ;
19
+ }
20
+
21
+ for (int m = 1 ; m <= numRows ; m ++) {
22
+ result .add (generateRow (m ));
23
+ }
24
+ return result ;
25
+ }
26
+
27
+ public List <Integer > generateRow (int row ) {
28
+ ArrayList <Integer > last = new ArrayList <>();
29
+ last .add (1 );
30
+
31
+ for (int i = 1 ; i < row ; i ++) {
32
+ for (int j = last .size () - 2 ; j >= 0 ; j --) {
33
+ last .set (j + 1 , last .get (j ) + last .get (j + 1 ));
34
+ }
35
+ last .add (1 );
36
+ }
37
+ return last ;
38
+ }
39
+
40
+ public static void main (String [] args ) {
41
+ System .out .println (new PascalTriangle ().generate (9 ));
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments