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>First(2, 1);
if(rowIndex==0) return vector<int>(1,1);
if(rowIndex==1) return First;
vector<int>lastLine = First;
vector<int>oneLine;
for (int i = 2; i <= rowIndex; i++){
oneLine.clear();
oneLine.push_back(1);
for (int j = 0; j<lastLine.size()-1; j++){
oneLine.push_back(lastLine[j] + lastLine[j + 1]);
}
oneLine.push_back(1);
lastLine = oneLine;
}
return oneLine;
}