public boolean containsDuplicate(int[] nums) {
final int length = nums.length;
Set<Integer> set = new HashSet<>();
for (int i = 0; i < length; i++) {
set.add(nums[i]);
if (set.size() != (i + 1)) {
return true;
}
}
return false;
}
public boolean containsDuplicate2(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int i : nums) {
if (set.contains(i)) {
return true;
}
set.add(i);
}
return false;
}
public boolean containsDuplicate3(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
网友评论