Leetcode之Remove Duplicates from Sorted List I II

26. Remove Duplicates from Sorted Array

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
Example 1:
Given 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 returned length.

Problem solving ideas

Use the fast and slow pointers to record the coordinates of the traversal. At the beginning, both pointers point to the first number. If the numbers pointed to by the two pointers are the same, the fast pointer moves forward one step. If they are different, both pointers move forward. One step, so that when the fast pointer walks through the entire array, the current coordinate of the slow pointer plus 1 is the number of different numbers in the array. At the same time, the title also requires O(1) space complexity to modify the original array in place so that there are no duplicate values.

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if (nums.empty()) return 0;
        int j = 0, n = nums.size(); //j为慢指针,i为快指针
        for (int i = 0; i < n; ++i) {
            if (nums[i] != nums[j]) nums[++j] = nums[i];
        }
        return j + 1;
    }
};

80. Remove Duplicates from Sorted Array II

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given 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 respectively.
It doesn’t matter what you leave beyond the returned length.

Problem solving ideas

This question is a continuation of the previous one, Remove Duplicates from Sorted Array. The maximum number of repetitions allowed here is two, so we need to use a variable count to record how many repetitions are allowed, count It is initialized to 1. If there is a repetition, the count will be decremented by 1. Then the next time there will be a repetition, the fast pointer will move forward directly. If it is not repeated at this time, the count will be restored to 1. Since the entire array is ordered, once If there is a non-repeated number, it must be larger than this number, and there will be no repeats after this number.

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.size()<=2)
            return nums.size();
        int pre=0,cur=1,count=1;
        while(cur<nums.size()){
            if(nums[pre]==nums[cur] && count == 0) //下次再出现重复,快指针直接前进一步
                ++cur;
            else{
                if(nums[pre]==nums[cur]) //如果出现过一次重复,则count递减1
                    --count;
                else
                    count=1;  //如果这时候不是重复的,则count恢复1
                nums[++pre]=nums[cur++];
            }
        }
        return pre+1;
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325600091&siteId=291194637