mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
Create 118. Pascal's Triangle.md
This commit is contained in:
parent
cecc32ec97
commit
f2da89fd4a
22
118. Pascal's Triangle.md
Normal file
22
118. Pascal's Triangle.md
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/description/)
|
||||||
|
# 思路
|
||||||
|
首先明白题目要求返回的是一个vector,其元素也是vector,按照题目规律构造每个vector即可。
|
||||||
|
# C++
|
||||||
|
```
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> generate(int numRows) {
|
||||||
|
vector<vector<int> > result;
|
||||||
|
for(int i = 0; i < numRows; i++){
|
||||||
|
vector<int> tmp;
|
||||||
|
tmp.push_back(1);
|
||||||
|
for(int j = 1; j < i; j++){
|
||||||
|
tmp.push_back(result[i-1][j-1] + result[i-1][j]);
|
||||||
|
}
|
||||||
|
if(i > 0) tmp.push_back(1);
|
||||||
|
result.push_back(tmp);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user