80. Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":
What if duplicates are allowed at mosttwice?

For example,
Given sorted arraynums=[1,1,1,2,2,3],

Your function should return length =5, with the first five elements ofnumsbeing1,1,2,2and3. It doesn't matter what you leave beyond the new length.

思路:还是双指针

public class Solution {
    public int removeDuplicates(int[] nums) {
        int curr = 0;
        for (int i=0;i<nums.length;i++){
            if (i<2||nums[i]>nums[curr-2]){
                nums[curr++] = nums[i];
            }
        }
        return curr;
    }
}

results matching ""

    No results matching ""