簡體   English   中英

26. 從排序數組中刪除重復項 - Java

[英]26. Remove Duplicates from Sorted Array - Java

問題:給定一個排序數組 nums,就地刪除重復項,使每個元素只出現一次並返回新的長度。

不要為另一個數組分配額外的空間,您必須通過使用 O(1) 額外的 memory 就地修改輸入數組來做到這一點。

public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
    if (nums[j] != nums[i]) {
        i++;
        nums[i] = nums[j];
    }
}
return i + 1;

}

return 語句在這里究竟做了什么。 return i + 1在這里是什么意思?

return i + 1返回有多少唯一整數。 我相信這是一個 Leetcode 問題,因為它就位, int[]是通過引用傳入的,Leetcode 想知道要檢查多少個數字(你應該把唯一的數字放在第一個i + 1點)。

如果你看這個問題,它說: 在此處輸入圖片說明 這意味着您返回數組的長度。

所以,如果你有數組[1,1,2,3,4,4] ,你會把它變成[1,2,3,4,...] ,其中...是剩下的大批。 但是,您返回4因為新數組的長度應該是4

希望這可以為您解決問題!

您的問題已經在這里得到解答; 除此之外,我們還可以從零開始並刪除第一個if語句:

使用b.java文件進行測試:

import java.util.*;

class Solution {
    public static final int removeDuplicates(
        final int[] nums
    ) {

        int i = 0;

        for (int num : nums)
            if (i == 0 || num > nums[i - 1]) {
                nums[i++] = num;
            }

        return i;
    }
}


class b {
    public static void main(String[] args) {
        System.out.println(new Solution().removeDuplicates(new int[] { 1, 1, 2}));
        System.out.println(new Solution().removeDuplicates(new int[] { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4}));
    }
}

印刷

2
5
  • 我以這種簡單的方式嘗試過。 這里時間復雜度為 O(n),空間復雜度為 O(1)。

     static int removeDuplicates(int[] nums){ if(nums.length == 0) { return 0; } int value = nums[0]; int lastIndex = 0; int count = 1; for (int i = 1; i < nums.length; i++) { if(nums[i] > value) { value = nums[i]; lastIndex = lastIndex+1; nums[lastIndex] = value; count++; } } return count; }
class Solution {
public int removeDuplicates(int[] nums) {
    int n = nums.length;
    if (n == 0 || n == 1)
        return n;
    
    int j = 0;
    for (int i=0; i<n-1; i++)
            if (nums[i]!= nums[i+1])
                nums[j++] = nums[i];
            
        
    nums[j++]=nums[n-1];
    return j;
}

}

public class RemoveDuplicateSortedArray {
    //Remove Duplicates from Sorted Array
    public static void main(String[] args) {
        int[] intArray = new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        int count = extracted(intArray);
        for (int i = 0; i < count; i++) {
            System.out.println(intArray[i]);
        }
    }

    private static int extracted(int[] intArray) {
        int size = intArray.length;
        int count = 1;
        if (size == 1) {
            return 1;
        } else if (size == 2) {
            if (intArray[0] == intArray[1]) {
                return 1;
            } else {
                return 2;
            }
        } else {
            for (int i = 0, j = i + 1; j < size; j++) {
                if (intArray[i] < intArray[j]) {
                    i++;
                    intArray[i] = intArray[j];
                    count++;
                }
            }
            return count;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM