forked from Ritesh25696/interviewBit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascal Triangle
More file actions
40 lines (33 loc) · 925 Bytes
/
Pascal Triangle
File metadata and controls
40 lines (33 loc) · 925 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
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]
]
***************************************************************************************************************************
vector<vector<int> > Solution::generate(int A) {
vector<vector<int>> res;
vector<int> row;
vector<int> newRow;
if(A == 0) return res;
row.push_back(1);
res.push_back(row);
if(A == 1 ) return res;
while(res.size() < A){
newRow.push_back(1);
for(int i=1; i<row.size() ; i++){
newRow.push_back(row[i]+row[i-1]);
}
newRow.push_back(1);
res.push_back(newRow);
row = newRow;
newRow.clear();
}
return res;
}