80. Remove Duplicates from Sorted Array II

public class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums.length<3)
            return nums.length;
        int i=1;
        int j=2;
        while(j<nums.length)
        {
            if(nums[i-1]==nums[i]&&nums[i]==nums[j])
                j++;
            else
            {
                i++;
                nums[i]=nums[j];
                j++;
            }
        }
        return i+1;
    }
}

 

public int removeDuplicates(int[] nums) {
    int i = 0;
    for (int n : nums)
        if (i < 2 || n > nums[i-2])
            nums[i++] = n;
    return i;
}

 

posted @ 2017-09-27 05:30  Weiyu Wang  阅读(103)  评论(0编辑  收藏  举报