-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path118.杨辉三角.js
65 lines (63 loc) · 1.09 KB
/
118.杨辉三角.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* @lc app=leetcode.cn id=118 lang=javascript
*
* [118] 杨辉三角
*
* https://leetcode-cn.com/problems/pascals-triangle/description/
*
* algorithms
* Easy (72.75%)
* Likes: 595
* Dislikes: 0
* Total Accepted: 217.8K
* Total Submissions: 298.1K
* Testcase Example: '5'
*
* 给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。
*
* 在「杨辉三角」中,每个数是它左上方和右上方的数的和。
*
*
*
*
*
* 示例 1:
*
*
* 输入: numRows = 5
* 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
*
*
* 示例 2:
*
*
* 输入: numRows = 1
* 输出: [[1]]
*
*
*
*
* 提示:
*
*
* 1
*
*
*/
// @lc code=start
/**
* @param {number} numRows
* @return {number[][]}
*/
var generate = function(numRows) {
const ret=new Array()
for(let i=1;i<=numRows;i++){
const row=new Array(i).fill(1)
for(let j=1;j<i-1;j++){
row[j]=ret[i-2][j-1]+ret[i-2][j]
}
ret.push(row)
}
return ret
};
// @lc code=end