BiruLyu
6/20/2017 - 8:06 AM

80. Remove Duplicates from Sorted Array II(#1).java

public class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums == null) return 0;
        if (nums.length < 3) return nums.length;
        int i = 0;
        int j = 0;
        int count = 1;
        while (j < nums.length) {
            if (j > 0 && nums[j] == nums[j - 1]) {
                count++;
            } else {
                count = 1;
            }
            if (count > 2) {
                j++;
                continue;
            }
            nums[i++] = nums[j++];
        }
        return i;
    }
}
class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums == null) return 0;
        if (nums.length < 3) return nums.length;
        int len = nums.length, res = 0;
        for (int i = 0; i < len; i++) {
            if (i > 1 && nums[i] == nums[res - 2]) continue;
            nums[res++] = nums[i];
        }
        return res;
    }
}