(217)-(Contains Duplicate)-(数组中是否包含重复值)-(HashSet进行处理)

(217)-(Contains Duplicate)-(数组中是否包含重复值)-(HashSet进行处理)

//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

public class Solution 
{
    public boolean containsDuplicate(int[] nums) 
    {
        //为空,0元素,或者1元素都肯定不会重复的啦
        if(nums==null || nums.length==0||nums.length==1)
        {
            return false;
        }
     
        Set<Integer> temp_set=new HashSet<Integer>();
         for(int i=0;i<nums.length;i++)
         {
            if(temp_set.contains(nums[i]))
            {
                return true;
            } 
            else
            {
                temp_set.add(nums[i]);
            }        
         }
        //都遍历到最后了,都木有出现,说明不会重复的
         return false;   
    }
}

 

posted @ 2015-07-25 19:27  爱吃萝卜干  阅读(105)  评论(0编辑  收藏  举报