LeetCode 80. Remove Duplicates from Sorted Array II(java)

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.

思路和27,28类似,用recursion的想法,递归求规律。
public int removeDuplicates(int[] nums) {
        //special case
        if (nums == null) return 0;
        //base case;
        if (nums.length <= 2) return nums.length;
        int len = 2;
        //general case
        for (int i = 2; i < nums.length; i++) {
            if (nums[i] != nums[len - 1] || nums[i] != nums[len - 2]) {
                nums[len] = nums[i];
                len++;
            }
        }
        return len;
    }

你可能感兴趣的:(Array)