File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 37
37
73|[ Set Matrix Zeroes] ( ./0073-set-matrix-zeroes.js ) |Medium|
38
38
88|[ Merge Sorted Array] ( ./0088-merge-sorted-array.js ) |Easy|
39
39
118|[ Pascal's Triangle] ( ./0118-pascals-triangle.js ) |Easy|
40
+ 119|[ Pascal's Triangle II] ( ./0119-pascals-triangle-ii.js ) |Easy|
40
41
121|[ Best Time to Buy and Sell Stock] ( ./0121-best-time-to-buy-and-sell-stock.js ) |Easy|
41
42
151|[ Reverse Words in a String] ( ./0151-reverse-words-in-a-string.js ) |Medium|
42
43
152|[ Maximum Product Subarray] ( ./0152-maximum-product-subarray.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 119. Pascal's Triangle II
3
+ * https://leetcode.com/problems/pascals-triangle-ii/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given an integer rowIndex, return the rowIndexth (0-indexed) row of
7
+ * the Pascal's triangle.
8
+ *
9
+ * In Pascal's triangle, each number is the sum of the two numbers
10
+ * directly above it as shown:
11
+ */
12
+
13
+ /**
14
+ * @param {number } rowIndex
15
+ * @return {number[] }
16
+ */
17
+ var getRow = function ( rowIndex ) {
18
+ return generateTriangle ( rowIndex + 1 ) [ rowIndex ] ;
19
+ } ;
20
+
21
+ function generateTriangle ( numRows ) {
22
+ const result = [ [ 1 ] ] ;
23
+
24
+ for ( let i = 1 ; i < numRows ; i ++ ) {
25
+ result [ i ] = [ 1 ] ;
26
+ result [ i - 1 ] . forEach ( ( value , j , prev ) => {
27
+ result [ i ] [ j + 1 ] = ( prev [ j + 1 ] + value ) || 1 ;
28
+ } ) ;
29
+ }
30
+
31
+ return result ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments