Skip to content

Commit 293abff

Browse files
committed
Add solution #119
1 parent 70c5048 commit 293abff

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

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

solutions/0119-pascals-triangle-ii.js

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

0 commit comments

Comments
 (0)