Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return
Return
[1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
Could you optimize your algorithm to use only O(k) extra space?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Solution { public: vector<int> getRow(int rowIndex) { vector<int> result; int n = rowIndex / 2; result.push_back(1); for(int i = 1; i <= n; i++){ long long next = (long(result[i - 1]) * long(rowIndex - i + 1)) / i; result.push_back(next); } if(rowIndex % 2 == 1){ int size = result.size(); for(int i = size - 1; i >= 0; i--){ result.push_back(result[i]); } }else{ int size = result.size(); for(int i = size - 2; i >= 0; i--){ result.push_back(result[i]); } } return result; } }; |
0 comments:
Post a Comment