forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main4.cpp
39 lines (30 loc) · 858 Bytes
/
main4.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
/// Source : https://leetcode.com/problems/pascals-triangle-ii/description/
/// Author : liuyubobobo
/// Time : 2018-08-10
#include <iostream>
#include <vector>
using namespace std;
/// Using Binomial Coefficients Recurrence Relationship
/// Using C(m, n) = (m - n + 1) / n * C(m, n - 1)
///
/// Time Complexity: O(rowIndex)
/// Space Complexity: O(rowIndex)
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res(rowIndex + 1, 1);
for(int i = 1; i <= (rowIndex + 1) / 2; i ++)
res[i] = res[rowIndex - i] = (long long)(rowIndex - i + 1) * res[i - 1] / i;
return res;
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
for(int i = 0 ; i < 10 ; i ++)
print_vec(Solution().getRow(i));
return 0;
}