LeetCode-04-217存在重复元素

LeetCode-04-217存在重复元素

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

hashset自带去重,虽然好像也没多大用
遍历数组,如果set里面没有就添加到set
如果已经有直接return true
如果没有先添加,数组遍历完就return false
上一篇:查看Linux进程文件路径


下一篇:217. 存在重复元素