leetcode 26|27|80. Remove Duplicates from Sorted Array 1|2 && 27.Remove Element

本文介绍了几种在不使用额外空间的情况下对已排序数组进行操作的方法,包括去除重复元素、移除特定值及允许最多两次重复的元素处理。通过两种不同的算法实现,提供了清晰的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

26. Remove Duplicates from Sorted Array 

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.
方法一:删除重复

方法二:不删除元素,而是把不重复的元素放到前面去。

class Solution {
public:
    int removeDuplicates(vector<int>& nums)
    {
        //way-1
        /*
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]==nums[i+1] && i+1<nums.size())
            {
                nums.erase(nums.begin()+i+1);
                i--;
            }
        }
        return nums.size();
        */
        
        //way-2
        if(nums.size()<2)
            return nums.size();
        int j=0;
        for(int i=1;i<nums.size();i++)
        {
            if(nums[i]!=nums[j])
                nums[++j]=nums[i];
        }
        return j+1;
        
        
    }
};


27. Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.
方法一:移除指定值的元素
方法二:交换,因为好像是要求不能删除

class Solution {
public:
    int removeElement(vector<int>& nums, int val) 
    {
      //way-1
      /*
      for(int i=0;i<nums.size();i++)
      {
          if(nums[i]==val)
          {
              nums.erase(nums.begin()+i);
              i--;
          }
      }
      return nums.size();  
      */
      
      //way-2
      int j=0;
      for(int i=0;i<nums.size();i++)
      {
          if(nums[i]!=val)
          {  
              nums[j]=nums[i];
              j++;
          }
      }
      return j;
    }
};


80. Remove Duplicates from Sorted Array II

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.

两个指针,pos左边存符合的,(pos,r)存不符合的,r之后存没判定的!

这种题就要想两个指针移动这种方法。就像之前的  sort colors。

class Solution {
public:
    int removeDuplicates(vector<int>& nums) 
    {
        if (nums.size() == 0)
            return 0;
        int pos = 0; //pos左边存符合的,(pos,r)存不符合的,r之后存没判定的
        int flag = 0;
        for (int r = 1; r < nums.size(); r++)
        {
            if (nums[r] != nums[pos])
            {
                swap(nums[r], nums[++pos]);
                flag = 0;
            }
            else if (flag == 0)
            {
                swap(nums[r], nums[++pos]);
                flag++;
            }
        }
        return pos+1;
    }
};





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值