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

杨辉三角

程序员文章站 2022-04-22 10:57:51
...

Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
杨辉三角
杨辉三角

class Solution {
public:
    vector<vector<int>> generate(int n) {
        vector<vector<int>> res;
        vector<int> v = {1};
        if (n==0) return res;
        res.push_back(v);
        for (int i = 1; i < n; ++i) {
            vector<int> v = {1};
            for (int j = 1; j < i; ++j) {
                int x = res[i-1][j-1]+res[i-1][j];
                v.push_back(x);
            }
            v.push_back(1);
            res.push_back(v);
        }
        return res;
    }
};
相关标签: leetcode c++