-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerate_Pascal_Triangle.cpp
More file actions
64 lines (60 loc) · 1.07 KB
/
Copy pathGenerate_Pascal_Triangle.cpp
File metadata and controls
64 lines (60 loc) · 1.07 KB
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
/*
Problem:
**********************************************************************************
*
* Given numRows, generate the first numRows of Pascal's triangle.
*
* For example, given numRows = 5,
* Return
*
* [
* [1],
* [1,1],
* [1,2,1],
* [1,3,3,1],
* [1,4,6,4,1]
* ]
*
*
**********************************************************************************
*/
#include<vector>
#include<iostream>
using namespace std;
vector<vector<int>> Generate(int row)
{
vector<vector<int>> Pascal;
for(int i=0; i<row; i++)
{
vector<int> v;
if(i==0)
{
v.push_back(1);
}
else
{
v.push_back(1);
for(int j=0; j<Pascal[i-1].size()-1; j++)
{
int temp = Pascal[i-1][j] + Pascal[i-1][j+1];
v.push_back(temp);
}
v.push_back(1);
}
Pascal.push_back(v);
}
return Pascal;
}
int main()
{
vector<vector<int>> test;
int n = 3;
test = Generate(n);
cout << test[0][0] << endl;
cout << test[1][0] << endl;
cout << test[1][1] << endl;
cout << test[2][0] << endl;
cout << test[2][1] << endl;
cout << test[2][2] << endl;
return 0;
}