1-4存在重复

HashSet
JAVA

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

Python

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        dic = {}
        for i in range(len(nums)):
            if nums[i] in dic:
                return True
            else:
                dic[nums[i]]=i
        return False

你可能感兴趣的:(1-4存在重复)