数组去重

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class Solution {

    /**
     * 排序数组去掉重复元素,保留一个
     */
    public static int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

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

        System.out.println(Arrays.toString(nums));

        return k;
    }

    public static int removeDuplicates1(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        if (nums.length == 1 || (nums.length == 2 && nums[0] == nums[1])) {
            return 1;
        }

        int i = 0, j = 1, len = nums.length;
        while (i < len && j < len) {
            while (nums[i] == nums[j] && j + 1 < len) {
                j++;
            }

            if (nums[i] != nums[j]) {
                i++;
                if (j - i >= 1) {
                    nums[i] = nums[j];
                }

                j++;
            } else {
                break;
            }
        }
        System.out.println(Arrays.toString(nums));
        return i + 1;
    }

    public static int removeDuplicates2(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        if (nums.length == 1 || (nums.length == 2 && nums[0] == nums[1])) {
            return 1;
        }

        Set<Integer> set = new TreeSet<>();
        for (int d : nums) {
            set.add(d);
        }
        int i = 0;
        for (int d : set) {
            nums[i++] = d;
        }

        return set.size();
    }

    /**
     * 排序数组去掉重复元素,保留N个
     */
    public static int removeDuplicates3(int[] nums, int n) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int index = n;
        for (int i = n; i < nums.length; i++) {
            if (nums[i] != nums[i - n]) {
                nums[index++] = nums[i];
            }
        }

        System.out.println(Arrays.toString(nums));

        return index;
    }

    /**
     * 排序数组去掉重复元素,一个不留
     */
    public static int removeDuplicates4(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int len = nums.length;
        int index = 0;

        for (int i = 0; i < nums.length; ) {
            int start = i;
            int end = i + 1;
            while (end < len && nums[end] == nums[start]) {
                end++;
            }

            if (start + 1 == end) {
                nums[index++] = nums[start];
                i++;
            } else {
                i = end;
            }
        }

        System.out.println(Arrays.toString(nums));
        return index;
    }

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

        System.out.println(removeDuplicates4(nums));
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值