【LeetCode】140.Pascal's Triangle II
阿新 • • 發佈:2018-12-21
題目描述(Easy)
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
Note that the row index starts from 0.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
題目連結
Example 1:
Input: 3 Output: [1,3,3,1]
演算法分析
直上而下生成,從右往左相加,尾部補1,細節題。
提交程式碼:
class Solution { public: vector<int> getRow(int rowIndex) { vector<int> result; for (int i = 0; i <= rowIndex; ++i) { for (int j = i - 1; j > 0; --j) { result[j] = result[j - 1] + result[j]; } result.push_back(1); } return result; } };