217. Contains Duplicate Java Solutin

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.

 

Subscribe to see which companies asked this question

 

public class Solution {
    public boolean containsDuplicate(int[] nums) {
        if(nums == null || nums.length == 1)
            return false;
        HashSet<Integer> hs = new HashSet<Integer>();
    // Set<Integer> hs = new HashSet<Integer>(); 最初使用Set超时,         //遂使用HashSet
        for(int i=0;i<nums.length;i++){
            if(hs.contains(nums[i]))
                return true;
            else{
                hs.add(nums[i]);
            }
        }
        return false;
    }
}

 

posted @ 2016-04-13 11:45  Miller1991  阅读(164)  评论(0编辑  收藏  举报