-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangle.cpp
More file actions
41 lines (32 loc) · 808 Bytes
/
PascalTriangle.cpp
File metadata and controls
41 lines (32 loc) · 808 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
/*
Given numRows, generate the first numRows of Pascal’s triangle.
Pascal’s triangle : To generate A[C] in row R, sum up A’[C] and A’[C-1] from previous row R - 1.
Example:
Given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
LINK: https://www.interviewbit.com/problems/pascal-triangle/
*/
vector<vector<int> > Solution::solve(int A) {
if(A == 0) return vector<vector<int> > {};
vector<vector<int> > res(A);
vector<int> temp{1};
res[0] = temp;
temp.clear();
for(int i = 1; i< A; i++){
temp.push_back(1);
for(int j = 1; j<i; j++){
temp.push_back(res[i-1][j] + res[i-1][j-1]);
}
temp.push_back(1);
res[i]= temp;
temp.clear();
}
return res;
}