1. 程式人生 > >[LeetCode]80. Remove Duplicates from Sorted Array II刪除數組中的重復值

[LeetCode]80. Remove Duplicates from Sorted Array II刪除數組中的重復值

return 指針 post || log sorted cat span etc

和第一題不同的地方是,容忍兩次重復

雖然題目上說只需要長度,但是否檢測的時候如果數組不跟著改變也是不行的

沒說清楚題意

自己是用雙指針做的,看了大神的答案更簡單

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

[LeetCode]80. Remove Duplicates from Sorted Array II刪除數組中的重復值