diff --git a/118. Pascal's Triangle.md b/118. Pascal's Triangle.md new file mode 100644 index 0000000..f68f837 --- /dev/null +++ b/118. Pascal's Triangle.md @@ -0,0 +1,22 @@ +# [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/description/) +# 思路 +首先明白题目要求返回的是一个vector,其元素也是vector,按照题目规律构造每个vector即可。 +# C++ +``` +class Solution { +public: + vector> generate(int numRows) { + vector > result; + for(int i = 0; i < numRows; i++){ + vector 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; + } +}; +```