leetcode-26&80 Remove Duplicates from Sorted Array I&II

26.
题干:
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.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

题目简单,只需要一个迭代器进行判断相邻两个值是否相等,如果相等删除一个即可。
注意:对于迭代器的删除操作一定小心,迭代器增减的位置,以及由于增减操作造成的迭代器失效的问题;注意判断vector是否为空;

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.begin() != nums.end())
        for(vector<int>::iterator itr= nums.begin()+1; itr != nums.end(); )
        {
            if(*itr == *(itr-1))
            nums.erase(itr);
            else
            itr++;
        }
        return nums.size();
    }
};

程序基本秒过,但是效率不高,时间为64ms;

vector的删除操作比较耗时。
本题不能直接再申请一块额外的内存用于保存不重复的值;
题干要求只要前length项为单独的不同项即可,至于后面的不管,因此可以用赋值的办法提高效率,舍弃删除的办法。
想法是:用两个迭代器,一个从头到尾扫,另一个记录按照不重复的原则进行赋值。这样,最多只需要赋值N次即可(没有重复的数),不用使用erase函数。速度快一些,37ms。

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
    if(nums.size() == 0 || nums.size() == 1)
    return nums.size();
    int length=1;//长度
    vector<int>::iterator cur= nums.begin()+1;
    for(vector<int>::iterator itr=nums.begin()+1; itr != nums.end(); itr++)
    {
        if(*itr != *(itr-1))
        {
            *cur = *itr;
            cur++;
            length++;
        }
    }
    return length;
    }
};

80.
Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.

题目要求与上一题差不多,唯一区别是重复数字可以保有两个。
需要一个标志来区分。
代码如下:20ms。

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.size() < 3)
        return nums.size();
        int len=1;
        bool flag = false;
        vector<int>::iterator itr;
        for(itr = nums.begin()+1; itr != nums.end(); )
        {
            if( *itr == *(itr-1) )
            {
                if( flag )
                {
                    nums.erase(itr);
                }
                else
                {
                len++;
                itr++;
                flag = true;
                }
            }
            else
            {
                len++;
                itr++;
                flag = false;
            }
        }
        return len;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值