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

資訊專欄INFORMATION COLUMN

leetcode26 remove duplicate

syoya / 2910人閱讀

摘要:題目要求輸入一個數(shù)組和一個值,刪除數(shù)組中等于該值得元素。但是在數(shù)組的容量非常大且數(shù)組中該數(shù)字出現(xiàn)頻率不高的情況下,使用兩個指針可以明顯減少程序遍歷數(shù)組的時間。

題目要求:輸入一個數(shù)組和一個值,刪除數(shù)組中等于該值得元素。不允許分配新的內(nèi)存空間(即不允許創(chuàng)建新的數(shù)組),允許數(shù)組中的元素的順序發(fā)生變化,只要該數(shù)組在返回長度前的值正確
例如:輸入nums = [3,2,2,3], val = 3,程序返回2,且nums數(shù)組的前兩個值均為2

使用一個指針
時間復(fù)雜度O(n), 空間復(fù)雜度O(1)

/**
 * @author rale
 * Given an array and a value, remove all instances of that value in place and return the new length.
 * Do not allocate extra space for another array, you must do this in place with constant memory.
 * The order of elements can be changed. It doesn"t matter what you leave beyond the new length.
 * Example:
 * Given input array nums = [3,2,2,3], val = 3
 * Your function should return length = 2, with the first two elements of nums being 2.
 */
public class RemoveElement {
    
    public int removeElement(int[] nums, int val) {
        int index = 0;
        for(int i = 0 ; i

使用兩個指針
時間復(fù)雜度O(n) 空間復(fù)雜度O(1)

/**
 * @author rale
 * Given an array and a value, remove all instances of that value in place and return the new length.
 * Do not allocate extra space for another array, you must do this in place with constant memory.
 * The order of elements can be changed. It doesn"t matter what you leave beyond the new length.
 * Example:
 * Given input array nums = [3,2,2,3], val = 3
 * Your function should return length = 2, with the first two elements of nums being 2.
 */
public class RemoveElement {
    
    public int removeElement(int[] nums, int val) {
        if(nums==null || nums.length==0){
            return 0;
        }
        int leftPointer = 0;
        int rightPointer = nums.length-1;
        while(leftPointer<=rightPointer){
            if(nums[rightPointer]==val){
                rightPointer--;
                continue;
            }
            if(nums[leftPointer]==val){
                nums[leftPointer] = nums[rightPointer];
                rightPointer--;
            }
            leftPointer++;
        }
        return rightPointer+1;
    }
}

leetcode的測試用例得出的性能分析發(fā)現(xiàn),使用一個指針比使用兩個指針的速度更快。但是在數(shù)組的容量非常大且數(shù)組中該數(shù)字出現(xiàn)頻率不高的情況下,使用兩個指針可以明顯減少程序遍歷數(shù)組的時間。
leetcode上也給出了參考答案


想要了解更多開發(fā)技術(shù),面試教程以及互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號!將會不定期的發(fā)放福利哦~

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

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

相關(guān)文章

  • 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
  • leetcode刷題記錄-【26 Remove Duplicates from Sorted Ar

    摘要:給定一個排序數(shù)組,你需要在原地刪除重復(fù)出現(xiàn)的元素,使得每個元素只出現(xiàn)一次,返回移除后數(shù)組的新長度。不要使用額外的數(shù)組空間,你必須在原地修改輸入數(shù)組并在使用額外空間的條件下完成。聲明兩個指針,為快指針,為慢指針如果遇到相同的數(shù),那么就跳過,。 給定一個排序數(shù)組,你需要在原地刪除重復(fù)出現(xiàn)的元素,使得每個元素只出現(xiàn)一次,返回移除后數(shù)組的新長度。不要使用額外的數(shù)組空間,你必須在原地修改輸入數(shù)組...

    heartFollower 評論0 收藏0
  • [LintCode/LeetCode] Remove Duplicate Letters

    Problem Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical...

    wanghui 評論0 收藏0

發(fā)表評論

0條評論

syoya

|高級講師

TA的文章

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