首页 > 其他分享 >LeetCode刷题:runtime error: reference binding to null pointer of type 'int' (stl_vector.h)报错

LeetCode刷题:runtime error: reference binding to null pointer of type 'int' (stl_vector.h)报错

时间:2023-01-15 11:34:09浏览次数:67  
标签:vector reference int res lastRight intervals 报错 lastLeft null

题目:https://leetcode.cn/problems/merge-intervals/
错误代码:

// 思路初探:做了很多道类似区间操作的题目了。本题就是尽可能少的创建新区间
// 1.首先对区间排序(左边界升序,右边界都行)
// 2.遍历合并
class Solution {
public:
    static bool myCmp(vector<int> a,vector<int> b){
        return a[0] <= b[0];
    }
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> res;
        if (intervals.size() == 0) return res;
        sort(intervals.begin(),intervals.end(),myCmp);
        int lastRight = intervals[0][1];
        int lastLeft = intervals[0][0];
        for(int i = 1; i < intervals.size();i++){
            int curLeft = intervals[i][0];
            int curRight = intervals[i][1];
            if(curLeft > lastRight){
                //创建新区间
                res.push_back(vector<int>{lastLeft,lastRight});
                lastLeft = curLeft;
                lastRight = curRight;
            }else{ //延长原来的区间
                lastRight = max(lastRight,curRight);
            }
        }
        res.push_back(vector<int>{lastLeft,lastRight});
        return res;
    }
};

报错提示:

标签:vector,reference,int,res,lastRight,intervals,报错,lastLeft,null
From: https://www.cnblogs.com/swx123/p/17053248.html

相关文章