成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Remove Duplicates from Sorted

WalkerXu / 2733人閱讀

摘要:思路原數(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個元素。循環(huán)整個數(shù)組,比較nums[i]nums[i-1]。將所有不重復的數(shù)值賦給nums[index],而當nums[i]nums[i-1]相等時,不做處理。最后返回的index就是不同元素的個數(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

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

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;
    }
}

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

轉載請注明本文地址:http://m.hztianpu.com/yun/65830.html

相關文章

  • [LintCode/LeetCode] Find Minimum in Rotated Sorted

    摘要:排序數(shù)組中找最小值或最大值的題目,很明顯可以使用二分法。因此,只判斷終點和中點元素的大小關系即可。這里有一種情況是上述后三個,中點值和末位相等。此時,兩邊同時遞歸,并返回兩邊遞歸值的較小值。當首位和末位重合,說明已夾逼得到最小值。 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 移除有

    摘要:雙指針法復雜度時間空間思路我們可以將不重復的序列存到數(shù)列前面,因為不重復序列的長度一定小于等于總序列,所以不用擔心覆蓋的問題。代碼雙指針法復雜度時間空間思路思路和上題一樣,區(qū)別在于記錄前兩個遍歷到的數(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

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

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

    摘要:思路與代碼其實在這里我們仍然延續(xù)中的思路。在遇到非重復值以及非多余的重復值時,將數(shù)值移動到當前記錄的下標上。保證該下標前的值均為滿足題目條件的值。第一次我使用了來記錄某個值出現(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元查看
<