题目
代码部分一(27ms 30.81%)
class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums == null || nums.length == 0) return false;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
map.put(nums[i], map.getOrDefault(nums[i], 0)+1);
if(map.get(nums[i]) > 1)
return true;
}
return false;
}
}
代码部分二(5ms 95.66%)
class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums == null || nums.length == 0) return false;
Arrays.sort(nums);
for(int i = 0; i < nums.length - 1; i++){
if(nums[i] == nums[i+1]) return true;
}
return false;
}
}