leetcodeブラシ問題、まとめ、記録、メモ119
903 ワード
leetcode119Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3, Return
Note: Could you optimize your algorithm to use only O(k) extra space?
Subscribe to see which companies asked this question
Pascal's Triangle Iと同様に、kのサイズに応じて、対応する反復を行えばよい.
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?
Subscribe to see which companies asked this question
Pascal's Triangle Iと同様に、kのサイズに応じて、対応する反復を行えばよい.
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> temp, t;
temp.push_back(1);
if (rowIndex == 0)
{
return temp;
}
for (int i = 1; i <= rowIndex; ++i)
{
temp.push_back(0);
t = temp;
for (int j = 1; j <= i; ++j)
{
t[j] = temp[j] + temp[j-1];
}
temp = t;
}
return t;
}
};