LeetCode119. Pascal's Triangle II

来源:互联网 发布:h3c交换机禁止mac 编辑:程序博客网 时间:2024/05/22 10:50

Description

Given an index k, return the kth row of the Pascal’s triangle.

For example, given k = 3,
Return [1,3,3,1].

my program

class Solution {public:    vector<int> getRow(int rowIndex) {        vector<int> res = {1};        for(int i = 1; i<=rowIndex; i++)        {            res.push_back(0);            for(int j = res.size(); j>0; j--)            {                res[j] += res[j-1];            }        }        return res;    }};

34 / 34 test cases passed.
Status: Accepted
Runtime: 0 ms

other methods

vector<int> getRow(int rowIndex) {    vector<int> ans(rowIndex+1,1);    int small = rowIndex/2;    long comb = 1;    int j = 1;    for (int i=rowIndex; i>=small; i--){        comb *= i;        comb /= j;        j ++;        ans[i-1] = (int)comb;        ans[j-1] = (int)comb;    }    return ans;}
0 0
原创粉丝点击