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?
1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 vector<int> res; 5 if(rowIndex < 0) 6 return res; 7 res.push_back(1); 8 if(rowIndex == 0) 9 return res; 10 res.push_back(1); 11 if(rowIndex == 1) 12 return res; 13 int tmp1 = 0; 14 int tmp2 = 0; 15 for(int i = 2; i <= rowIndex; i++) 16 { 17 tmp2 = 1; 18 for(int j = 1; j < i; j++) 19 { 20 tmp1 = tmp2; 21 tmp2 = res[j]; 22 res[j] = tmp1+tmp2; 23 } 24 res.push_back(1); 25 } 26 return res; 27 } 28 };