-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path119.杨辉三角-ii.js
79 lines (77 loc) · 1.28 KB
/
119.杨辉三角-ii.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* @lc app=leetcode.cn id=119 lang=javascript
*
* [119] 杨辉三角 II
*
* https://leetcode-cn.com/problems/pascals-triangle-ii/description/
*
* algorithms
* Easy (66.44%)
* Likes: 325
* Dislikes: 0
* Total Accepted: 145.6K
* Total Submissions: 218.3K
* Testcase Example: '3'
*
* 给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。
*
* 在「杨辉三角」中,每个数是它左上方和右上方的数的和。
*
*
*
*
*
* 示例 1:
*
*
* 输入: rowIndex = 3
* 输出: [1,3,3,1]
*
*
* 示例 2:
*
*
* 输入: rowIndex = 0
* 输出: [1]
*
*
* 示例 3:
*
*
* 输入: rowIndex = 1
* 输出: [1,1]
*
*
*
*
* 提示:
*
*
* 0
*
*
*
*
* 进阶:
*
* 你可以优化你的算法到 O(rowIndex) 空间复杂度吗?
*
*/
// @lc code=start
/**
* @param {number} rowIndex
* @return {number[]}
*/
var getRow = function(rowIndex) {
const rowNums=rowIndex+1
const ret=new Array()
for(let i=1;i<=rowNums;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[rowIndex]
};
// @lc code=end