[LeetCode] 26. Remove Duplicates from Sorted Array 有序數組中去除重復項


 

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);

// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

 

這道題要我們從有序數組中去除重復項,和之前那道 Remove Duplicates from Sorted List 的題很類似,但是要簡單一些,因為畢竟數組的值可以通過下標直接訪問,而鏈表不行。那么這道題的解題思路是使用快慢指針來記錄遍歷的坐標,最開始時兩個指針都指向第一個數字,如果兩個指針指的數字相同,則快指針向前走一步,如果不同,則兩個指針都向前走一步,這樣當快指針走完整個數組后,慢指針當前的坐標加1就是數組中不同數字的個數,代碼如下:

 

解法一:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int pre = 0, cur = 0, n = nums.size();
        while (cur < n) {
            if (nums[pre] == nums[cur]) ++cur;
            else nums[++pre] = nums[cur++];
        }
        return nums.empty() ? 0 : (pre + 1);
    }
};

 

我們也可以用 for 循環來寫,這里的j就是上面解法中的 pre,i就是 cur,所以本質上都是一樣的,參見代碼如下:

 

解法二:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int j = 0, n = nums.size();
        for (int i = 0; i < n; ++i) {
            if (nums[i] != nums[j]) nums[++j] = nums[i];
        }
        return nums.empty() ? 0 : (j + 1);
    }
};

 

這里也可以換一種寫法,用變量i表示當前覆蓋到到位置,由於不能有重復數字,則只需要用當前數字 num 跟上一個覆蓋到到數字 nums[i-1] 做個比較,只要 num 大,則一定不會有重復(前提是數組必須有序),參見代碼如下:

 

解法三:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int i = 0;
        for (int num : nums) {
            if (i < 1 || num > nums[i - 1]) {
                nums[i++] = num;
            }
        }
        return i;
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/26

 

類似題目:

Remove Duplicates from Sorted List

Remove Duplicates from Sorted List II

Remove Duplicates from Sorted Array II

Remove Element

 

類似題目:

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

https://leetcode.com/problems/remove-duplicates-from-sorted-array/discuss/11757/My-Solution-%3A-Time-O(n)-Space-O(1)

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM