Created
October 31, 2019 16:14
-
-
Save f3lixding/d36ef051ab27dbf369ecea0417bbeacf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<vector<int>> generate(int numRows) { | |
vector<vector<int>> res; | |
vector<int> lastRow; | |
lastRow.push_back(1); | |
res.push_back(lastRow); | |
if (!numRows) { | |
return {}; | |
} | |
for (int i = 1; i < numRows; i++) { | |
vector<int> newRow; | |
for (int j = 0; j < lastRow.size() + 1; j++) { | |
if (j == 0 || j == lastRow.size()) { | |
newRow.push_back(1); | |
continue; | |
} | |
newRow.push_back(lastRow[j-1] + lastRow[j]); | |
} | |
res.push_back(newRow); | |
lastRow = newRow; | |
} | |
return res; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment