forked from livingstream/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangle.cpp
More file actions
50 lines (46 loc) · 1.04 KB
/
PascalTriangle.cpp
File metadata and controls
50 lines (46 loc) · 1.04 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
//============================================================================
// Pascal's Triangle
// 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 <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
vector<vector<int> > generate(int numRows)
{
vector<vector<int> > res;
if (numRows < 1) return res;
res.reserve(numRows);
res.push_back(vector<int>(1, 1));
int m = 1;
while (m < numRows)
{
vector<int> row;
row.reserve(m+1);
row.push_back(1);
for (int i = 0; i < m-1; i++)
row.push_back(res[m-1][i] + res[m-1][i+1]);
row.push_back(1);
res.push_back(row);
m++;
}
return res;
}
};
int main()
{
return 0;
}