[leetcode]Pascal's triangle 2
阿新 • • 發佈:2018-12-18
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.
分析:
楊輝三角要求第k行(k從0開始)的值,楊輝三角每一行的首尾的值都為1,每一行值的個數依次加一。且第k行(k>=2)的第j個值(1<=j<k)為第k-1行的第j和第j-1個值相加所得。
class Solution { public: vector<int> getRow(int rowIndex) { vector<int> res; for (int k = 0; k <= rowIndex; k++) { vector<int> curr(k+1, 1);//定義一個長度為k+1,值都為1的一維陣列 for(int j = 1; j < k; j++) { curr[j] = res[j-1]+res[j]; } res = curr; } return res; } };