# Leetcode 118. Pascal's Triangle ###### tags: `Leetcode(C++)` 題目 : https://leetcode.com/problems/pascals-triangle/ 。 想法 : 恩 ... 我以為是在練習帕斯卡,結果是在練習Vector。 時間複雜度 : O(n^2)。 程式碼 : ``` class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector<int> > ans; vector<int> tmp; tmp.push_back(1); ans.push_back(tmp); for(int i=1 ; i<numRows ; i++){ vector<int> tmp2; for(int j=0 ; j<=i ; j++){ if(j == i || j == 0){ tmp2.push_back(1); } else{ tmp2.push_back(ans[i-1][j] + ans[i-1][j-1]); } } ans.push_back(tmp2); } return ans; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up