LetCode 118. 杨辉三角

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

static int x=[](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();

 

你可能感兴趣的:(LetCode)