Sharing my simple and clear C++ solution


  • 0
    Z
    vector<vector<int> > generate(int numRows) {
        vector<vector<int>> result;
        if(numRows < 1){
            return result;
        }
        vector<int> line;
        line.push_back(1);
        result.push_back(line);
        if(numRows == 1){
            return result;
        }
        for(int i = 2; i <= numRows; i++){
            line.clear();
            line.push_back(1);
            for(int j = 0; j < i - 2; j++){
                line.push_back(result[i - 2][j] + result[i - 2][j + 1]);
            }
            line.push_back(1);
            result.push_back(line);
        }
        return result;
    }

Log in to reply
 

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