【LeetCode】119. Pascal's Triangle II
Pascal's Triangle II
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?
与Pascal's Triangle思路相同,只返回最后一行
class Solution { public: vector<int> getRow(int rowIndex) { vector<int> cur(1,1); vector<int> last = cur; for(int i = 1; i <= rowIndex; i ++) {// i_th level last.push_back(0); cur = last; for(int j = 1; j <= i; j ++) { cur[j] = last[j] + last[j-1]; } last = cur; } return cur; } };