Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://leetcode.com/onlinejudge#question_119
- /*
- Given an index k, return the kth row of the Pascal's triangle.
- For example, given k = 3,
- Return [1,3,3,1].
- Note:
- Could you optimize your algorithm to use only O(k) extra space?
- */
- class Solution {
- public:
- vector<int> getRow(const int rowIndex) {
- typedef std::vector<int> row_t;
- typedef std::vector<row_t> tri_t;
- row_t prev(rowIndex+1,1), curr(rowIndex+1,1);
- for(int row_no = 0; row_no < rowIndex+1; ++row_no)
- {
- std::swap(prev,curr);
- curr[0] = curr[row_no] = 1;
- for(int j = 1; j < row_no; ++j)
- {
- curr[j] = prev[j-1] + prev[j];
- }
- }
- return curr;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment