leetcode_question_119 Pascal's Triangle II

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?

vector<int> getRow(int rowIndex) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> matrix(rowIndex+1);
if(rowIndex < 0) return matrix;
for(int i = 0; i <= rowIndex; ++i)
{
if( i == 0) {matrix[0] = 1;continue;}
int mid = i/2;
int tmpnum = matrix[0];
for(int j = 1; j <= mid; ++j)
{
int tmp = tmpnum + matrix[j];
tmpnum = matrix[j];
matrix[j] = tmp;
}
++mid;
while(mid <= i){
matrix[mid] = matrix[i-mid];
++mid;
}
}
return matrix;
}
上一篇:BFC和haslayout


下一篇:Qt5制作鼠标悬停显示Hint的ToolTip