Leetcode80. Remove Duplicates from Sorted Array II

这题和
26
Remove Duplicates from Sorted Array 40.0% Easy
80
Remove Duplicates from Sorted Array II 39.7% Medium
83
Remove Duplicates from Sorted List 42.1% Easy
82
Remove Duplicates from Sorted List II
共同构成一个系列。看似沙雕,其实还挺有意思的。

class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums==null||nums.length<1) return 0;
        
        int index = 0;
        for(int num:nums){
            if(index<2||num>nums[index-2])
                nums[index++]=num;
        }
        return index;
    }
}

Runtime: 1 ms, faster than 99.95% of Java online submissions for Remove Duplicates from Sorted Array II.
Memory Usage: 38.2 MB, less than 94.72% of Java online submissions for Remove Duplicates from Sorted Array II.

posted @ 2019-03-28 00:08  大胖子球花  阅读(82)  评论(0)    收藏  举报