-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0119.杨辉三角-ii.cpp
54 lines (51 loc) · 1.06 KB
/
0119.杨辉三角-ii.cpp
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
/*
* @lc app=leetcode.cn id=119 lang=cpp
*
* [119] 杨辉三角 II
*
* https://leetcode-cn.com/problems/pascals-triangle-ii/description/
*
* algorithms
* Easy (58.88%)
* Likes: 91
* Dislikes: 0
* Total Accepted: 29.1K
* Total Submissions: 49.4K
* Testcase Example: '3'
*
* 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
*
*
*
* 在杨辉三角中,每个数是它左上方和右上方的数的和。
*
* 示例:
*
* 输入: 3
* 输出: [1,3,3,1]
*
*
* 进阶:
*
* 你可以优化你的算法到 O(k) 空间复杂度吗?
*
*/
// @lc code=start
#include <vector>
using namespace std;
class Solution {
public:
vector<int> getRow(int rowIndex) {
if (rowIndex == 0) return {1};
vector<int> res = {1,1};
for (int i = 1; i < rowIndex; i++) {
vector<int> tmp(i+2, 1);
for (int j = 1; j < tmp.size()-1; j++) {
tmp[j] = res[j-1] + res[j];
}
res = tmp;
}
return res;
}
};
// @lc code=end