Skip to content

Instantly share code, notes, and snippets.

@f3lixding
Created October 31, 2019 16:14
Show Gist options
  • Save f3lixding/d36ef051ab27dbf369ecea0417bbeacf to your computer and use it in GitHub Desktop.
Save f3lixding/d36ef051ab27dbf369ecea0417bbeacf to your computer and use it in GitHub Desktop.
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