Pascal's Triangle II 生成楊輝三角中的某行
阿新 • • 發佈:2019-02-03
題目:
解答:
在上一題基礎上修改,注意行號。
程式碼:
class Solution { public: vector<int> getRow(int rowIndex) { vector<vector<int> > result; vector<int> temp; if (rowIndex < 0) return temp; temp.push_back(1); result.push_back(temp); for (int i = 1; i <= rowIndex; i++) { temp.clear(); for (int j = 0; j <= i; j++) { if (j == 0) temp.push_back(result[i - 1][j]); else if (j == i) temp.push_back(result[i - 1][j - 1]); else { temp.push_back(result[i - 1][j] + result[i - 1][j - 1]); } } result.push_back(temp); } return temp; } };