forked from livingstream/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangleII.cpp
More file actions
42 lines (38 loc) · 918 Bytes
/
PascalTriangleII.cpp
File metadata and controls
42 lines (38 loc) · 918 Bytes
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
//============================================================================
// Pascal's Triangle II
// Given an index k, return the kth row of the Pascal's triangle.
//
// For example, given k = 3,
// Return [1,3,3,1].
//
// Note:
// Could you optimize your algorithm to use only O(k) extra space?
//============================================================================
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
vector<int> getRow(int rowIndex)
{
vector<int> res;
res.reserve(rowIndex+1);
res.push_back(1);
if (rowIndex < 1) return res;
res.push_back(1);
int m = 1;
while (m < rowIndex)
{
for (int i = 0; i < m; i++)
res[i] += res[i+1];
res.insert(res.begin(), 1);
m++;
}
return res;
}
};
int main()
{
return 0;
}