LeetCode/118. Pascal's Triangle.md
2018-09-02 15:14:58 +08:00

23 lines
684 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [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;
}
};
```