119. 杨辉三角 II
1、倒着相加
class Solution {
public:
vector<int> getRow(int rowIndex)
{
vector<int> result;
for(int i = 0; i <= rowIndex; ++i)
{
result.push_back(1);
for(int j = i - 1; j > 0; --j)
{
result[j] += result[j - 1];
}
}
return result;
}
};
class Solution {
public:
vector<int> getRow(int rowIndex)
{
vector<int> row(rowIndex + 1);
row[0] = 1;
for (int i = 1; i <= rowIndex; ++i)
{
row[i] = 1LL * row[i - 1] * (rowIndex - i + 1) / i; //(n-m+1)/m 1LL是表示1 (这个1是 long long 类型)
}
return row;
}
};