c++ - 合并间隔/行 1034 : Char 9: runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int>>'

标签 c++ vector

leetcode 问题-->我要计算常见/重叠间隔
任何人都可以帮助为什么会出现这个错误。
问题链接-https://leetcode.com/problems/merge-intervals/
我的代码是

    class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>>ans;
        int n = intervals.size();
        ans[0][0] = intervals[0][0];
        ans[0][1] = intervals[0][1];
        int i=0,j=1;
        while(j<n ){
            if(ans[i][1] > intervals[j][0]){
                //ans[i][0] = intervals[i][0];
                ans[i][1] = intervals[j][1];
                j++;
            }
            else{
                i=i+1;
                ans[i][0] = intervals[j][0];
                ans[i][1] = intervals[j][1];
                j++;
            }
            
        }
        return ans;
    }
};

最佳答案

首先,您需要为您的问题选择一个更好的标题。其次,我相信你的算法不满足全部可能的条件;如果有一组像 {[2,3], [1,6]} 这样的区间,您的算法会将这些区间合并到 [2,6]。因此,最好对您的算法进行一些修改。

关于c++ - 合并间隔/行 1034 : Char 9: runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int>>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64399457/

相关文章:

c++ - O(n) 比 O(nlogn) 花费更多的时间

c++ - 负无穷大

c++ - 如何在 MFC 多字节应用程序中显示西里尔文字?

r - 如何将两个相同长度的向量合并为一个在R中也具有相同长度的向量

c++ - 在 C++ 中从堆栈中弹出一个数字?

c++ - Rust与C++ : Returning objects from functions

c++ - vector::erase() 未按预期工作

c++ - C++程序中的 vector 错误

C++无法得到中值的正确答案

c++ - 如何在排序的 vector 上正确应用 erase remove idiom?