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;
}
本文介绍了几种用于查找数组中是否存在重复元素的有效算法,包括简单的排序方法、使用哈希集合进行快速查找,以及针对特定条件(如元素之间的最大距离)的复杂情况下的解决方案。
469

被折叠的 条评论
为什么被折叠?



