Created
January 16, 2015 06:19
-
-
Save codinfox/04911d165fbbc0bc173c to your computer and use it in GitHub Desktop.
This file contains 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
//C++11 | |
class Solution { | |
public: | |
vector<vector<int> > generate(int numRows) { | |
if (numRows <= 0) return vector<vector<int> >(); | |
vector<vector<int> > result; | |
result.push_back(vector<int>{1}); | |
if (numRows == 1) return result; | |
result.push_back(vector<int>{1, 1}); | |
if (numRows == 2) return result; | |
for (int counter = 2; counter < numRows; counter++) { | |
auto const& prev = result[counter - 1]; | |
vector<int> newRow{1}; | |
auto start = prev.begin() + 1, end = prev.end(); | |
while (start != end) { | |
newRow.push_back(*start + *(start-1)); | |
start++; | |
} | |
newRow.push_back(1); | |
result.push_back(newRow); | |
} | |
return result; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment