217. 存在重复元素 给定一个整数数组,判断是否存在重复元素。 如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。

在这里插入图片描述

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

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/gps666666/article/details/120233356