[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?
和118基本没区别吧……
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 vector<int> vec_a(1, 1); 7 vector<int> vec_b; 8 vector<int> *vec_last = NULL, *vec_cur = NULL; 9 bool flag = true; 10 if (0 == rowIndex) { 11 return vec_a; 12 } 13 for (int i = 1; i <= rowIndex; ++i) { 14 if (flag) { 15 vec_last = &vec_a; 16 vec_cur = &vec_b; 17 } else { 18 vec_last = &vec_b; 19 vec_cur = &vec_a; 20 } 21 vec_cur->clear(); 22 23 for (int j = 0; j <= i; ++j) { 24 vec_cur->push_back((0 == j ? 0 : (*vec_last)[j - 1]) 25 + (i == j ? 0 : (*vec_last)[j])); 26 } 27 flag = !flag; 28 } 29 return *vec_cur; 30 } 31 };