File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .sanket .Array ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+
6
+ /*
7
+ Problem Link - https://leetcode.com/problems/pascals-triangle/
8
+ Status - Accepted
9
+ */
10
+ public class PascalTriangle {
11
+
12
+ public static void main (String [] args ) {
13
+ int num = 3 ;
14
+ generate (num );
15
+ }
16
+
17
+ public static List <List <Integer >> generate (int numRows ) {
18
+ List <List <Integer >> lists = new ArrayList <>();
19
+ if (numRows < 1 ) { return lists ; }
20
+ List <Integer > first = new ArrayList <>();
21
+ first .add (1 );
22
+ lists .add (first );
23
+ int i = 1 ;
24
+ while (i < numRows ) {
25
+ List <Integer > integers = new ArrayList <>();
26
+ for (int j = 0 ; j <= i ; j ++) {
27
+ if (j == 0 || j == i ) {
28
+ integers .add (1 );
29
+ }
30
+ else {
31
+ List <Integer > prev = lists .get (i - 1 );
32
+ int total = prev .get (j - 1 ) + prev .get (j );
33
+ integers .add (total );
34
+ }
35
+ }
36
+ lists .add (integers );
37
+ i ++;
38
+ }
39
+ return lists ;
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments