Skip to content

Instantly share code, notes, and snippets.

@DrynnBavis
Created February 2, 2019 00:31
Show Gist options
  • Select an option

  • Save DrynnBavis/6bfd691762a1134fb63595682f6ae144 to your computer and use it in GitHub Desktop.

Select an option

Save DrynnBavis/6bfd691762a1134fb63595682f6ae144 to your computer and use it in GitHub Desktop.
c++ solution to Pascal Pyramid
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
if(!numRows) return {};
result.push_back({1});
for(int i = 0; i < numRows-1; i++){
vector<int> row;
vector<int> prevRow = result.back();
for(int k = 0; k < prevRow.size(); k++){
int rightNum = prevRow[k];
int leftNum = (k == 0) ? 0 : prevRow[k-1];
row.push_back(rightNum + leftNum);
}
row.push_back(1);
result.push_back(row);
}
return(result);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment