File tree 2 files changed +28
-0
lines changed
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change 36
36
67|[ Add Binary] ( ./0067-add-binary.js ) |Easy|
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
+ 118|[ Pascal's Triangle] ( ./0118-pascals-triangle.js ) |Easy|
39
40
121|[ Best Time to Buy and Sell Stock] ( ./0121-best-time-to-buy-and-sell-stock.js ) |Easy|
40
41
151|[ Reverse Words in a String] ( ./0151-reverse-words-in-a-string.js ) |Medium|
41
42
152|[ Maximum Product Subarray] ( ./0152-maximum-product-subarray.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 118. Pascal's Triangle
3
+ * https://leetcode.com/problems/pascals-triangle/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given an integer numRows, return the first numRows of Pascal's triangle.
7
+ *
8
+ * In Pascal's triangle, each number is the sum of the two numbers directly
9
+ * above it as shown:
10
+ */
11
+
12
+ /**
13
+ * @param {number } numRows
14
+ * @return {number[][] }
15
+ */
16
+ var generate = function ( numRows ) {
17
+ const result = [ [ 1 ] ] ;
18
+
19
+ for ( let i = 1 ; i < numRows ; i ++ ) {
20
+ result [ i ] = [ 1 ] ;
21
+ result [ i - 1 ] . forEach ( ( value , j , prev ) => {
22
+ result [ i ] [ j + 1 ] = ( prev [ j + 1 ] + value ) || 1 ;
23
+ } ) ;
24
+ }
25
+
26
+ return result ;
27
+ } ;
You can’t perform that action at this time.
0 commit comments