美文网首页
[数组]217. Contains Duplicate

[数组]217. Contains Duplicate

作者: Reflection_ | 来源:发表于2017-10-24 14:17 被阅读0次

    题目: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.

    如果数组中有重复的数字就返回true,否则返回false。

    用hashset查重即可。

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

    相关文章

      网友评论

          本文标题:[数组]217. Contains Duplicate

          本文链接:https://www.haomeiwen.com/subject/evgtpxtx.html