Created
February 2, 2019 00:31
-
-
Save DrynnBavis/6bfd691762a1134fb63595682f6ae144 to your computer and use it in GitHub Desktop.
c++ solution to Pascal Pyramid
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>> 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