Skip to content

Instantly share code, notes, and snippets.

@codinfox
Created January 16, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codinfox/04911d165fbbc0bc173c to your computer and use it in GitHub Desktop.
Save codinfox/04911d165fbbc0bc173c to your computer and use it in GitHub Desktop.
//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