Skip to content

Commit d910064

Browse files
committed
feat: add solution of Pascals Triangle(118) with javascript.
1 parent 08471e1 commit d910064

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
| [110][110-question] | [Balanced Binary Tree][110-tips] | [Easy][E] | [][110-java] | [][110-js] | [][110-kotlin] |
8282
| [111][111-question] | [Minimum Depth of Binary Tree][111-tips] | [Easy][E] | [][111-java] | [][111-js] | [][111-kotlin] |
8383
| [112][112-question] | [Path Sum][112-tips] | [Easy][E] | [][112-java] | [][112-js] | [][112-kotlin] |
84-
| [118][118-question] | [Pascal's Triangle][118-tips] | [Easy][E] | [][118-java] | | [][118-kotlin] |
84+
| [118][118-question] | [Pascal's Triangle][118-tips] | [Easy][E] | [][118-java] | [][118-js] | [][118-kotlin] |
8585
| [119][119-question] | [Pascal's Triangle II][119-tips] | [Easy][E] | [][119-java] | | [][119-kotlin] |
8686
| [121][121-question] | [Best Time to Buy and Sell Stock][121-tips] | [Easy][E] | [][121-java] | | [][121-kotlin] |
8787
| [122][122-question] | [Best Time to Buy and Sell Stock II][122-tips] | [Easy][E] | [][122-java] | | [][122-kotlin] |
@@ -461,6 +461,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
461461
[110-js]: ./src/_110/Solution.js
462462
[111-js]: ./src/_111/Solution.js
463463
[112-js]: ./src/_112/Solution.js
464+
[118-js]: ./src/_118/Solution.js
464465
[226-js]: ./src/_226/Solution.js
465466
[561-js]: ./src/_561/Solution.js
466467
[643-js]: ./src/_643/Solution.js

src/_118/Solution.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number} numRows
3+
* @return {number[][]}
4+
*/
5+
var generate = function(numRows) {
6+
let arr = []
7+
for(var i = 1; i <= numRows; i++) {
8+
if(i === 1) {
9+
arr.push([1])
10+
continue
11+
}
12+
if(i === 2) {
13+
arr.push([1, 1])
14+
continue
15+
}
16+
let innerArr = [1]
17+
for(var j = 2; j < i; j++) {
18+
innerArr.push(arr[i-2][j-2] + arr[i-2][j-1])
19+
}
20+
innerArr.push(1)
21+
arr.push(innerArr)
22+
}
23+
return arr
24+
};

tips/118/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ class Solution {
6969
}
7070
```
7171

72+
```JavaScript
73+
var generate = function(numRows) {
74+
let arr = []
75+
for(var i = 1; i <= numRows; i++) {
76+
if(i === 1) {
77+
arr.push([1])
78+
continue
79+
}
80+
if(i === 2) {
81+
arr.push([1, 1])
82+
continue
83+
}
84+
let innerArr = [1]
85+
for(var j = 2; j < i; j++) {
86+
innerArr.push(arr[i-2][j-2] + arr[i-2][j-1])
87+
}
88+
innerArr.push(1)
89+
arr.push(innerArr)
90+
}
91+
return arr
92+
};
93+
```
7294
## 结语
7395

7496
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]

0 commit comments

Comments
 (0)