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?
Solution: from back to forth...
1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 vector<int> res(rowIndex+1, 1); 5 for(int i = 1; i <= rowIndex; i++) { 6 for(int j = i-1; j >= 1; j--) { 7 res[j] += res[j-1]; 8 } 9 } 10 return res; 11 } 12 };