[LeetCode]217. Contains Duplicate&219. Contains Duplicate II&220. Contains Duplicate III

217 . Contains Duplicate
Easy

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

5ms:

public boolean containsDuplicate(int[] nums) {
        Arrays.sort(nums);
        for(int i=0;i<nums.length-1;i++){
            if(nums[i]==nums[i+1]) return true;
        }
        return false;
}

13ms:

public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for(int num : nums){
            if(set.contains(num)) return true;
            else set.add(num);
        }
        return false;
}

219 . Contains Duplicate II
Easy

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.

10ms:

public boolean containsNearbyDuplicate(int[] nums, int k) {
       Set<Integer> set = new HashSet<Integer>();
       for(int i = 0; i < nums.length; i++){
           if(i > k) set.remove(nums[i-k-1]);
           if(!set.add(nums[i])) return true;
       }
       return false;
}

220 . Contains Duplicate III
Medium

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.

10ms:

public class Solution {
    TreeNode root = null;
    boolean flag = false;

    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        for (int i = 0; i < nums.length; i++) {
            if (i > k) 
                root = delete(root, nums[i - k - 1]);

            root = insert(root, nums[i], t);
            if (flag) return true;
        }
        return false;
    }

    public TreeNode insert(TreeNode root, int num, int t) {
        if (root == null) {
            TreeNode curNode = new TreeNode(num);
            return curNode;
        }

        if (Math.abs((long) (root.val - num)) <= t) {
            flag = true;
            return root;
        }

        if (root.val < num) {
            root.right = insert(root.right, num, t);
        } else if (root.val > num) {
            root.left = insert(root.left, num, t);
        }

        return root;
    }

    public TreeNode delete(TreeNode root, int num) {
        if (root == null) return null;

        if (root.val < num) {
            root.right = delete(root.right, num);
        } else if (root.val > num) {
            root.left = delete(root.left, num);
        } else {
            if (root.left == null || root.right == null) {
                return root.left == null ? root.right : root.left;
            } else {
                //this sense that left and right is none null
                //this is a easy method to deal with it.
                //that exchange the root val with the min or max node
                root.val = findMin(root.right).val;
                root.right = delete(root.right, root.val);
            }
        }

        return root;
    }

    public TreeNode findMin(TreeNode node) {
        if (node == null) return null;
        while (node.left != null) {
            node = node.left;
        }
        return node;
    }

    class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int x) {
            val = x;
        }
    }
}

5ms:


 public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if (k < 1 || t < 0)
            return false;

        long range;
        long max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
        for (int num : nums) {
            max = max > num ? max : num;
            min = min < num ? min : num;
        }
        range = max - min + 1;
        long bucket_with = ((long) t) + 1;
        int size = (int) (range / bucket_with + 1);
        int[] buckets = new int[size];
        Arrays.fill(buckets, -1);

        for (int i = 0; i < nums.length; i++) {
            int bucket_idx = (int) ((((long) nums[i]) - min + 1) / bucket_with );
            if (buckets[bucket_idx] >= 0) {
                return true;
            }
            buckets[bucket_idx] = i;

            if (bucket_idx > 0) {
                int j = buckets[bucket_idx-1];
                if (j >= 0 && Math.abs(((long) nums[i]) - nums[j]) <= t) {
                    return true;
                }
            }

            if (bucket_idx < size-1) {
                int j = buckets[bucket_idx + 1];
                if (j >= 0 && Math.abs(((long) nums[i]) - nums[j]) <= t) {
                    return true;
                }
            }

            if (i >= k) {
                buckets[(int) ((((long) nums[i-k]) - min + 1) / bucket_with )] = -1;
            }
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值