Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- http://leetcode.com/onlinejudge#question_118
- Given numRows, generate the first numRows of Pascal's triangle.
- For example, given numRows = 5,
- Return
- [
- [1],
- [1,1],
- [1,2,1],
- [1,3,3,1],
- [1,4,6,4,1]
- ]
- */
- class Solution {
- public:
- vector<vector<int> > generate(int numRows) {
- typedef vector<int> row_t;
- typedef vector<row_t> tri_t;
- tri_t tri;
- for(size_t row_no = 0; row_no < numRows; ++row_no)
- {
- const size_t row_size = row_no + 1;
- tri.push_back(row_t(row_size));
- tri[row_no][0] = tri[row_no][row_no] = 1;
- for(size_t pos = 1; pos < row_no; ++pos)
- {
- tri[row_no][pos] =
- tri[row_no-1][pos-1] + tri[row_no-1][pos];
- }
- }
- return tri;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment