Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
已排列好的数据,将重复数值移出
class Solution {
public:
int removeDuplicates(vector& nums) {
vector::iterator it;
for(it=nums.begin();it!=nums.end()-1;++it)
{
if(*it==*(it+1))
it=nums.erase(it)-1;///erase-1
}
return *it;
}
};
问题:
结果是对的,但是提交的时候显示:Runtime Error Message: reference binding to null pointer of type 'int'
很少看到这里使用erase函数,是有什么降低速度之类的缺点吗?
谢谢
http://blog.csdn.net/makuiyu/article/details/43406551
if(*it==*(it+1)),
当it=num.end()-1时,it+1指向null pointer。
楼上的方法挺好的。