LeetCode刷题系列(六)Remove Duplicates

本文详细探讨了如何从有序和无序的Array与List中删除重复元素,包括已排序的Array、已排序的List、未排序的List等场景。通过具体的LeetCode题目,解释了不同情况下的解决方案,涉及双指针法、链表操作以及HashSet的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  本篇主要是从Array和List中remove duplicates,其中Array和List有已经排好序的和未排序的。

Remove Duplicates from Sorted Array

  题目为删除有序数组中重复的元素。有序数组中的重复元素一定是与之前元素相等,我们很容易能判别出重复元素,但是我们删除元素时,数组后面的元素需要向前移动,这里我们使用了两个局部变量,一个i记录从头要访问的元素,一个记录需要保留在数组中元素,这样所有的元素相当于只进行了一次移动,避免增加时间复杂度。代码:

public class Solution {
    public int removeDuplicates(int[] A) {
        if (A == null || A.length == 0) {
            return 0;
        }

        int size = 0;
        for (int i = 0; i < A.length; i++) {
            if (A[i] != A[size]) {
                A[++size] = A[i];
            }
        }
        return size + 1;
    }
}

Remove Duplicates from Sorted Array II

  此题目与Remove Duplicates from Sorted Array I的区别在于这道题目允许存在两次重复,即如果某个元素出现两次,则不进行删除。代码:

public int removeDuplicates(int[] nums) {
    if(nums == null)
        return 0;
    int cur = 0;
    int i ,j;
    for(i = 0; i < nums.length;){
        int now = nums[i];
        for( j = i; j < nums.length; j++){
            if(nums[j] != now)
                break;
            if(j-i < 2)
                nums[cur++] = now; 
        }
        i = j;
    }
    return cur;
}

  其中if(j-i < 2)使得程序允许出现两次重复,如果两次以上,则j直接掉过,然后i = j,直接把两次以上重复的元素全部覆盖掉。

Remove Duplicates from Sorted List

  这道题目为删除有序链表中的重复元素。虽然代码不同,但是思路与Remove Duplicates from Sorted Array很相似。比较容易检测到重复元素,然后将重复元素的前一个元素的next指向重复元素之后的元素删除掉重复元素即可。同时,注意一下边界检查,代码:

public ListNode deleteDuplicates(ListNode head) {
    if (head == null) {
        return null;
    }

    ListNode node = head;
    while (node.next != null) {
        if (node.val == node.next.val) {
            node.next = node.next.next;
        } else {
            node = node.next;
        }
    }
    return head;
}

Remove Duplicates from Sorted List II

  此题目与Remove Duplicates from Sorted List I的区别在与将所有重复的元素全部删掉,不保留任何一个副本。如之前blog的介绍,当头元素也有可能被操作时(这里是被删除),我们需要添加dummyNode作为Head,代码:

public ListNode deleteDuplicates(ListNode head) {
    if(head == null || head.next == null)
        return head;

    ListNode dummy = new ListNode(0);
    dummy.next = head;
    head = dummy;

    while (head.next != null && head.next.next != null) {
        if (head.next.val == head.next.next.val) {
            int val = head.next.val;
            while (head.next != null && head.next.val == val) {
                head.next = head.next.next;
            }            
        } else {
            head = head.next;
        }
    }

    return dummy.next;
}

  这里注意一下边界检查,同时此判断语句head.next != null && head.next.next != null是由于如果head.next.next == null了,那么head.next一定不是重复元素(也是由于if中的while循环导致的)。

Remove Duplicates from Unsorted List

  最后一道题目是在非有序链表中删除重复元素,类似这样的题目,我们就需要花费力气在判断一个元素是否是重复元素上了,因此我们使用了一个HashSet,将所有元素扔进set中,然后调用其contains函数即可知道该元素是否为重复元素,我们也可以使用HashMap,不过需要使用其多个函数,而Set本身存在即是为了判断有哪些元素的,因而类似的问题我们均可使用HashSet(Set中较为常用的一种,时间效率较高)。另外,确定好重复元素之后将其删除与Remove Duplicates from Sorted List I思路类似,这里就不给出代码了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值