讨论/《初级算法》 - 删除排序数组中的重复项/
《初级算法》 - 删除排序数组中的重复项

c++求助

我这个到底是哪里错了?

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.empty())return 0;
        vector<int>::iterator head = nums.begin();
        for(vector<int>::iterator it = head+1;it!=nums.end();){
            while(*it==*head){
                if(nums.size()==1)return 1;
                it = nums.erase(it);
            }
            head=it++;
        }
        return nums.size();
    }
};
=================================================================
==42==ERROR: AddressSanitizer: negative-size-param: (size=-4)
    #5 0x7f3055fe20b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x6020000005bc is located 12 bytes inside of 16-byte region [0x6020000005b0,0x6020000005c0)
allocated by thread T0 here:
    #6 0x7f3055fe20b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
==42==ABORTING
展开全部 255 讨论