Remove Duplicates from Sorted Array

这道题在Leetcode上分为两个部分,分别是题号为26和80的两道题

Part One:先来看26题:

Remove Duplicates from Sorted Array
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.

For example,
Given input array A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

标签: Array Two Pointers
两个指针这个真的很重要!以前这种问题基本都会先想到从后向前冲刷的那种方法效率比较低,用两个指针会提升很多。


/** 
 * @author joker
 */

public class Solution {

    public static int removeDuplicates(int[] nums) {

        //消除一样的数字
        if(nums.length == 0 || nums.length ==1){
            return nums.length;
        }

        int point = 0;

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

    }

    public static void main(String[] args) {
        int[] array = {1,1,1,2};

        int length = removeDuplicates(array);

        for(int i = 0 ; i < length; i++)
            System.out.println(array[i]);
    }


}

Part Two:80题
Remove Duplicates from Sorted Array II
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].

标签: Array Two Pointers
分析

加一个变量记录一下元素出现的次数即可。这题因为是已经排序的数组,所以一个变量即可解决。如果是没有排序的数组,则需要引入一个hashmap来记录出现次数。

首先放一下我写的代码:

/**
 * 
 * @author joker
 *
 */
public class Solution {
    public static int removeDuplicates(int[] nums) {

        //消除一样的数字
        if(nums.length == 0 || nums.length ==1){
            return nums.length;
        }

        int point = 0;

        int pre;

        boolean flag = false;

        for(int i = 1 ; i < nums.length ; i++)
        {
            if(nums[i]!=nums[point])
            {
                nums[++point] = nums[i];
                flag = true;
                pre = nums[i];
            }
            else if(flag && nums[i] == nums[point])
            {
                nums[++point] = nums[point - 1];
                flag = !flag;
            }
            else if(nums[i] == nums[point] && point == 0)
            {
                nums[++point] = nums[i];
            }

        }
        return point + 1;

    }

    public static void main(String[] args) {
        int[] array = {1,1,2,2,2,3,3};

        int length = removeDuplicates(array);

        for(int i = 0 ; i < length; i++)
            System.out.println(array[i]);
        }
    }

下面是ACM之家大神们写的代码:
代码1:这个代码将每个序列看成两个没有重复元素的序列组合在一起的形式,每次都跟index-2的元素进行比较


// LeetCode, Remove Duplicates from Sorted Array II
// 时间复杂度O(n),空间复杂度O(1)
// @author hex108 (https://github.com/hex108)
class Solution {
public:
    int removeDuplicates(int A[], int n) {
        if (n <= 2) return n;

        int index = 2;
        for (int i = 2; i < n; i++){
            if (A[i] != A[index - 2])
                A[index++] = A[i];
        }

        return index;
    }
};

代码2:这个比较好想到,就是在遍历序列的时候加上计数变量,当计数值大于2的时候index不改变

public class Solution {
    public static int removeDuplicates(int[] A) {
        if(A.length <= 2) return A.length;
        int dup = 1;
        int index = 1;
        for(int i=1; i<A.length; i++){
            if(A[i] == A[i-1])
                dup++;
            else
                dup = 1;
            A[index] = A[i];
            if(dup <= 2){
                index++;
            }
        }
        return index;
    }
}

详细内容见网址:http://www.acmerblog.com/leetcode-solution-remove-duplicates-from-sorted-array-ii-6238.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值