标签:
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> row; 5 row.push_back(1); 6 if(rowIndex == 0) return row; 7 8 row.push_back(1); 9 if(rowIndex == 1) return row; 10 11 for(int i = 2; i <= rowIndex; i++){ 12 vector<int> temp; 13 temp.push_back(1); 14 for(int j = 1; j < i; j++) 15 temp.push_back(row[j-1] + row[j]); 16 temp.push_back(1); 17 row = temp; 18 } 19 return row; 20 } 21 };
标签:
原文地址:http://www.cnblogs.com/amazingzoe/p/4422000.html