Share my c++ code using STL transform


  • 0
    G
    class Solution {
    public:
        vector<vector<int>> generate(int numRows) {
            vector<vector<int>> result;
            vector<int> row = {1, 0};
            for (int i = 0; i < numRows; ++i) {
                transform(row.begin(), prev(row.end()), next(row.begin()), 
                    row.begin(), plus<int>());
                result.push_back(vector<int>(row.begin(), prev(row.end())));
                row.insert(row.begin(), 0);
            }
            return result;
        }
    };
    

    The values of row after being transformed in each iteration are as follows:

    0: {1, 0}

    ---{0, 1, 0}

    1: {1, 1, 0}

    ---{0, 1, 1, 0}

    2: {1, 2, 1, 0}

    ---{0, 1, 2, 1, 0}

    3: {1, 3, 3, 1, 0}

    ---{0, 1, 3, 3, 1, 0}

    ...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.