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