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?
class Solution {
public:
vector<int> getRow(int rowIndex) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> vec(rowIndex+1,1);
if(rowIndex <= 1) return vec;
for(int i = 2; i <= rowIndex;i++)
{
for(int j = i-1; j >=1 ;j--)
{
vec[j] += vec[j-1];
}
}
return vec;
}
};
16 milli secs.
因为不需要返回所有数组,只用一个O(rowIndex)的数组即可。