成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[Leetcode] Remove Duplicates from Sorted Array 移除有

kel / 468人閱讀

摘要:雙指針法復(fù)雜度時間空間思路我們可以將不重復(fù)的序列存到數(shù)列前面,因為不重復(fù)序列的長度一定小于等于總序列,所以不用擔(dān)心覆蓋的問題。代碼雙指針法復(fù)雜度時間空間思路思路和上題一樣,區(qū)別在于記錄前兩個遍歷到的數(shù)字來幫助我們判斷是否出現(xiàn)了第三遍。

Remove Duplicates from Sorted Array I

Given a sorted array, 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 in place with constant memory.

For example, Given input array 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 new length.

雙指針法 復(fù)雜度

時間 O(N) 空間 O(1)

思路

我們可以將不重復(fù)的序列存到數(shù)列前面,因為不重復(fù)序列的長度一定小于等于總序列,所以不用擔(dān)心覆蓋的問題。具體來說,用兩個指針,快指針指向當(dāng)前數(shù)組遍歷到的位置,慢指針指向不重復(fù)序列下一個存放的位置,這樣我們一旦遍歷到一個不重復(fù)的元素,就把它復(fù)制到不重復(fù)序列的末尾。判斷重復(fù)的方法是記錄上一個遍歷到的數(shù)字,看是否一樣。

代碼
public class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums.length == 0) return 0;
        int dup = nums[0];
        int end = 1;
        for(int i = 1; i < nums.length; i++){
            if(nums[i]!=dup){
                nums[end] = nums[i];
                dup = nums[i];
                end++;
            }
        }
        return end;
    }
}
Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?

For example, Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn"t matter what you leave beyond the new length.

雙指針法 復(fù)雜度

時間 O(N) 空間 O(1)

思路

思路和上題一樣,區(qū)別在于記錄前兩個遍歷到的數(shù)字來幫助我們判斷是否出現(xiàn)了第三遍。如果當(dāng)前數(shù)字和前一個數(shù)字的前一個一樣的話,說明出現(xiàn)了第三次。

代碼
public class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums.length <= 2) return nums.length;
        int dup1 = nums[0];
        int dup2 = nums[1];
        int end = 2;
        for(int i = 2; i < nums.length; i++){
            if(nums[i]!=dup1){
                nums[end] = nums[i];
                dup1 = dup2;
                dup2 = nums[i];
                end++;
            }
        }
        return end;
    }
}
Follow Up 后續(xù)

Q:如果數(shù)組沒有排序的話如何解?
A:可以先將其排序再用同樣方法求解,然而時間復(fù)雜度將達(dá)到O(NlogN),如果我們改用哈希表或集合來記錄數(shù)字出現(xiàn)次數(shù),可以用O(N)空間得到O(N)的時間。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/64521.html

相關(guān)文章

  • [LintCode/LeetCode] Remove Duplicates from Sorted

    摘要:思路原數(shù)組長度為,則返回原數(shù)組長度不為,則至少有個元素。將所有不重復(fù)的數(shù)值賦給,而當(dāng)和相等時,不做處理。最后返回的就是不同元素的個數(shù),也是新數(shù)組的長度。只有在時,才對賦值。注意,每次初始化的時候要分兩種情況,這就意味著從的時候開始遍歷。 Remove Duplicates from Sorted Array I Problem Given a sorted array, remove ...

    WalkerXu 評論0 收藏0
  • leetcode80. Remove Duplicates from Sorted Array II

    摘要:思路與代碼其實在這里我們?nèi)匀谎永m(xù)中的思路。在遇到非重復(fù)值以及非多余的重復(fù)值時,將數(shù)值移動到當(dāng)前記錄的下標(biāo)上。保證該下標(biāo)前的值均為滿足題目條件的值。第一次我使用了來記錄某個值出現(xiàn)的次數(shù)。 題目要求 Follow up for Remove Duplicates: What if duplicates are allowed at most twice? For example, Giv...

    CoderDock 評論0 收藏0
  • leetcode 26 Remove Duplicates from Sorted Array

    摘要:題目比較簡單,就是找出數(shù)組不重復(fù)的數(shù)字,返回不重復(fù)的數(shù)字個數(shù)。無需刪除重復(fù)數(shù)字,只需要保證數(shù)組的前位為不重復(fù)的個數(shù)字即可代碼如下 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not all...

    alaege 評論0 收藏0
  • ?LeetCode 26:刪除排序數(shù)組中的重復(fù)項 Remove Duplicates from So

    給定一個排序數(shù)組,你需要在原地刪除重復(fù)出現(xiàn)的元素,使得每個元素只出現(xiàn)一次,返回移除后數(shù)組的新長度。 不要使用額外的數(shù)組空間,你必須在原地修改輸入數(shù)組并在使用 O(1) 額外空間的條件下完成。 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and re...

    Alan 評論0 收藏0
  • ?LeetCode 26:刪除排序數(shù)組中的重復(fù)項 Remove Duplicates from So

    給定一個排序數(shù)組,你需要在原地刪除重復(fù)出現(xiàn)的元素,使得每個元素只出現(xiàn)一次,返回移除后數(shù)組的新長度。 不要使用額外的數(shù)組空間,你必須在原地修改輸入數(shù)組并在使用 O(1) 額外空間的條件下完成。 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and re...

    cnTomato 評論0 收藏0

發(fā)表評論

0條評論

kel

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<