Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从排序数组中删除重复项:我的代码在leetcode上提交时抛出运行时错误,但它为所有测试用例提供了正确答案?_C++_Vector - Fatal编程技术网

C++ 从排序数组中删除重复项:我的代码在leetcode上提交时抛出运行时错误,但它为所有测试用例提供了正确答案?

C++ 从排序数组中删除重复项:我的代码在leetcode上提交时抛出运行时错误,但它为所有测试用例提供了正确答案?,c++,vector,C++,Vector,问题:从排序数组中删除重复项 我的代码: 您忘记了使用空向量进行测试。您的代码似乎在计数,而不是删除。这些名字相当混乱。这是一个选项。也许你可以在这里找到一个提示: class Solution { public: int removeDuplicates(vector<int>& nums) { int count = 1; auto remove_duplicate = nums.begin();

问题:从排序数组中删除重复项 我的代码:


您忘记了使用空向量进行测试。您的代码似乎在计数,而不是删除。这些名字相当混乱。这是一个选项。也许你可以在这里找到一个提示:
class Solution {
public:

        int removeDuplicates(vector<int>& nums) {
            int count = 1;
            auto remove_duplicate = nums.begin();
            auto check_duplicate = nums.begin()+1;
            while(check_duplicate != nums.end()){
                if(*check_duplicate != *remove_duplicate){
                    remove_duplicate++;
                    if(check_duplicate != remove_duplicate){
                        *remove_duplicate = *check_duplicate;
                    }
                    count++;
                }
                check_duplicate++;
            }
            return count;
        }
    };
Line 851: Char 45: runtime error: applying non-zero offset 4 to null pointer (stl_iterator.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_iterator.h:860:45