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

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Remove Duplicates from Sorted

WalkerXu / 2704人閱讀

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

Remove Duplicates from Sorted Array I Problem

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.

Example

Given input array A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

Note

思路:原數(shù)組長度為0,則返回0;原數(shù)組長度不為0,則至少有index = 1個(gè)元素。循環(huán)整個(gè)數(shù)組,比較nums[i]nums[i-1]。將所有不重復(fù)的數(shù)值賦給nums[index],而當(dāng)nums[i]nums[i-1]相等時(shí),不做處理。最后返回的index就是不同元素的個(gè)數(shù),也是新數(shù)組的長度。

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

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

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

Your function should return length = 5, and A is now [1,1,2,2,3].

Note

相同的思路,加入一個(gè)count變量,每次循環(huán)比較前后元素是否重復(fù)時(shí),更新該元素出現(xiàn)的次數(shù)count。只有在count <= 2時(shí),才對nums[index]賦值。
注意,每次初始化count的時(shí)候要分兩種情況:i == 0 || nums[i] != nums[i-1],這就意味著從i = 0的時(shí)候開始遍歷。

Solution
public class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums.length == 0) return 0;
        int index = 0, count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i == 0 || nums[i] != nums[i-1]) count = 1;
            else count++;
            if (count <= 2) {
                nums[index] = nums[i];
                index++;
            }
        }
        return index;
    }
}

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

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

相關(guān)文章

  • [LintCode/LeetCode] Find Minimum in Rotated Sorted

    摘要:排序數(shù)組中找最小值或最大值的題目,很明顯可以使用二分法。因此,只判斷終點(diǎn)和中點(diǎn)元素的大小關(guān)系即可。這里有一種情況是上述后三個(gè),中點(diǎn)值和末位相等。此時(shí),兩邊同時(shí)遞歸,并返回兩邊遞歸值的較小值。當(dāng)首位和末位重合,說明已夾逼得到最小值。 Find Minimum in Rotated Sorted Array Problem Suppose a sorted array is rotated...

    cgh1999520 評論0 收藏0
  • [LintCode/LeetCode] Search Insert Position

    Problem Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume NO duplicates in the a...

    cjie 評論0 收藏0
  • [Leetcode] Remove Duplicates from Sorted Array 移除有

    摘要:雙指針法復(fù)雜度時(shí)間空間思路我們可以將不重復(fù)的序列存到數(shù)列前面,因?yàn)椴恢貜?fù)序列的長度一定小于等于總序列,所以不用擔(dān)心覆蓋的問題。代碼雙指針法復(fù)雜度時(shí)間空間思路思路和上題一樣,區(qū)別在于記錄前兩個(gè)遍歷到的數(shù)字來幫助我們判斷是否出現(xiàn)了第三遍。 Remove Duplicates from Sorted Array I Given a sorted array, remove the dupl...

    kel 評論0 收藏0
  • leetcode82. Remove Duplicates from Sorted List II

    摘要:題目要求翻譯將鏈表中重復(fù)的元素全部刪除,返回新的頭結(jié)點(diǎn)。相比于,這里將重復(fù)的元素全部刪除。除此以外,我們還需要知道重復(fù)元素的前一個(gè)值和重復(fù)元素的最后一個(gè)值。如果存在重復(fù)值,則跳過重復(fù)值后,前節(jié)點(diǎn)不變,否則前節(jié)點(diǎn)跟隨后節(jié)點(diǎn)同時(shí)向后移動。 題目要求 Given a sorted linked list, delete all nodes that have duplicate number...

    崔曉明 評論0 收藏0
  • leetcode80. Remove Duplicates from Sorted Array II

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

    CoderDock 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<