-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path118.杨辉三角.cpp
More file actions
32 lines (30 loc) · 762 Bytes
/
118.杨辉三角.cpp
File metadata and controls
32 lines (30 loc) · 762 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
/*
* @lc app=leetcode.cn id=118 lang=cpp
*
* [118] 杨辉三角
*/
// @lc code=start
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>>& result = *new vector<vector<int>>;
result.resize(numRows);
for (int i = 1; i <= numRows; i++)
{
for (int j = 1; j <= i; j++)
{
if (j == 1 || j == i)
{
result.at(i - 1).push_back(1);
}
else
{
int sum = result.at(i - 2).at(j - 2) + result.at(i - 2).at(j - 1);
result.at(i - 1).push_back(sum);
}
}
}
return result;
}
};
// @lc code=end