题目描述:
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> result; 5 result.push_back(1); 6 7 ++rowIndex; 8 long s = 1; 9 if (rowIndex == 1) { 10 return result; 11 } 12 13 for (int j = 1; j <= rowIndex - 2; ++j) { 14 result.push_back(s = ((rowIndex - j) * s) / j); 15 } 16 17 result.push_back(1); 18 19 return result; 20 } 21 };