简体   繁体   English

26. 从排序数组中删除重复项 - Java

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

Question: Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.问题:给定一个排序数组 nums,就地删除重复项,使每个元素只出现一次并返回新的长度。

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.不要为另一个数组分配额外的空间,您必须通过使用 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;

} }

What exactly does the return statement do here. return 语句在这里究竟做了什么。 What does return i + 1 mean here? return i + 1在这里是什么意思?

The return i + 1 is returning how many unique integers are there. return i + 1返回有多少唯一整数。 I believe this is a Leetcode problem, and since its in place, the int[] is passed in by reference, Leetcode wants to know how many numbers to check (you're supposed to put the unique numbers in the first i + 1 spots).我相信这是一个 Leetcode 问题,因为它就位, int[]是通过引用传入的,Leetcode 想知道要检查多少个数字(你应该把唯一的数字放在第一个i + 1点)。

If you look at the question, it says:如果你看这个问题,它说: 在此处输入图片说明 Which means that you return the length of the array.这意味着您返回数组的长度。

So, if you have the array [1,1,2,3,4,4] , you would turn that into [1,2,3,4,...] , where the ... is the rest of the array.所以,如果你有数组[1,1,2,3,4,4] ,你会把它变成[1,2,3,4,...] ,其中...是剩下的大批。 However, you return 4 because the length of the new array should be 4 .但是,您返回4因为新数组的长度应该是4

Hope this clears things up for you!希望这可以为您解决问题!

Your question has been already answered here ;您的问题已经在这里得到解答; in addition to that, we can also start from zero and remove the first if statement:除此之外,我们还可以从零开始并删除第一个if语句:

Test with a b.java file:使用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}));
    }
}

prints印刷

2
5
  • I tried in this easy way.我以这种简单的方式尝试过。 Here Time complexity is O(n) and space complexity: O(1).这里时间复杂度为 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