Skip to content

Commit 70c5048

Browse files
committed
Add solution #118
1 parent b50563d commit 70c5048

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
67|[Add Binary](./0067-add-binary.js)|Easy|
3737
73|[Set Matrix Zeroes](./0073-set-matrix-zeroes.js)|Medium|
3838
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
39+
118|[Pascal's Triangle](./0118-pascals-triangle.js)|Easy|
3940
121|[Best Time to Buy and Sell Stock](./0121-best-time-to-buy-and-sell-stock.js)|Easy|
4041
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
4142
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|

solutions/0118-pascals-triangle.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
};

0 commit comments

Comments
 (0)