-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascalTriangle.cpp
More file actions
42 lines (39 loc) · 909 Bytes
/
pascalTriangle.cpp
File metadata and controls
42 lines (39 loc) · 909 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
/**
*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]
*]
*/
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int> > res;
if(numRows == 0)
return res;
vector<int> pasc;
pasc.push_back(1);
res.push_back(pasc);
if(numRows == 1)
return res;
pasc.push_back(1);
res.push_back(pasc);
if(numRows == 2)
return res;
for(int i = 3; i <= numRows; i++)
{
vector<int> temp ;
temp.push_back(1);
for(int j = 1; j < i-1; j++)
temp.push_back(pasc[j-1]+pasc[j]);
temp.push_back(1);
res.push_back(temp);
pasc = temp;
}
return res;
}
};