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

来源:互联网 发布:淘宝客服主管提成方案 编辑:程序博客网 时间:2024/04/25 12:21

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;    }};





阅读全文
0 0