LeetCode刷题:runtime error: reference binding to null pointer of type 'int' (stl_vector.h)报错请教
题目: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;
}
};
报错提示:
Line 1034: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9