File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
src/main/java/interview/google Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ package interview .google ;
2+
3+ import java .util .ArrayList ;
4+
5+ import org .junit .Test ;
6+
7+ import junit .framework .TestCase ;
8+
9+ /**
10+ * Link: https://www.interviewbit.com/problems/kth-row-of-pascals-triangle/
11+ *
12+ * @author shivam.maharshi
13+ */
14+ public class KthRowOfPascalsTriangle extends TestCase {
15+
16+ @ Test
17+ public static void test () {
18+ assertEquals (0 , getRow (-1 ).size ());
19+ assertEquals (1 , getRow (0 ).size ());
20+ assertEquals (2 , getRow (1 ).size ());
21+ assertEquals (3 , getRow (2 ).size ());
22+ assertEquals (4 , getRow (3 ).size ());
23+ assertEquals (5 , getRow (4 ).size ());
24+ assertEquals (6 , getRow (5 ).size ());
25+ }
26+
27+ public static ArrayList <Integer > getRow (int a ) {
28+ ArrayList <Integer > l = new ArrayList <>(), t = new ArrayList <>();
29+ if (a < 0 )
30+ return l ;
31+ int i = 1 ;
32+ t .add (1 );
33+ while (i <= a ) {
34+ l .add (1 );
35+ for (int j = 0 ; j < i - 1 ; j ++)
36+ l .add (t .get (j ) + t .get (j + 1 ));
37+ l .add (1 );
38+ t = l ;
39+ l = new ArrayList <>();
40+ i ++;
41+ }
42+ return t ;
43+ }
44+
45+ }
You can’t perform that action at this time.
0 commit comments