欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[LeetCode]119. Pascal's Triangle II

程序员文章站 2022-04-01 12:05:36
...

[LeetCode]119. Pascal’s Triangle II

题目描述

[LeetCode]119. Pascal's Triangle II

思路

直接计算二项式系数会出现乘法上溢
考虑动态规划

代码

#include <iostream>
#include <vector>

using namespace std;

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

int main() {
    Solution s;
    vector<int> res = s.getRow(13);

    for (int num : res)
        cout << num << " ";
    cout << endl;

    system("pause");
    return 0;
}
相关标签: leetcode