Leetcode26. Remove Duplicates from Sorted Array

这题和
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 == 0) return 0;
        
        int index=0;//index represents the pos to fill the num that is not the duplicate one
        for(int num:nums){
            if(index<1||num>nums[index-1])
                nums[index++]=num;
        }
        return index;
    }
}

Runtime: 1 ms, faster than 100.00% of Java online submissions for Remove Duplicates from Sorted Array.
Memory Usage: 41 MB, less than 82.74% of Java online submissions for Remove Duplicates from Sorted Array.

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