当前位置:网站首页>118. Yanghui triangle (dynamic planning)

118. Yanghui triangle (dynamic planning)

2022-06-12 17:39:00 Yangpengwei

Given a nonnegative integer numRows, Generate 「 Yang hui triangle 」 Before numRows That's ok . stay 「 Yang hui triangle 」 in , Each number is the sum of the numbers at the top left and right of it .

Ideas : The above topic has told us that this is a dynamic planning topic .

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> ret(numRows);
        for (int i = 0; i < numRows; ++i) {
            ret[i].resize(i + 1);
            ret[i][0] = ret[i][i] = 1;
            for (int j = 1; j < i; ++j) {
                ret[i][j] = ret[i - 1][j] + ret[i - 1][j - 1];
            }
        }
        return ret;
    }
};
原网站

版权声明
本文为[Yangpengwei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121731438329.html