leetcode学习笔记17

217. Contains Duplicate

Given an array of integers, find if the array contains any duplicates.

Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

class Solution {
    public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> hs=new HashSet<Integer>();
        for(int i=0;i<nums.length;i++){
            if(!hs.add(nums[i]))
                return true;
        }
        return false;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38941866/article/details/84966636
今日推荐