您的位置:首页 > 大数据 > 人工智能

LeetCode题解:Contains Duplicate

2015-08-12 11:08 337 查看
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.

题意:给定一个整数数组,判断是否含有重复元素

解决思路:利用Set

代码:

public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
         for(int i : nums)
             if(!set.add(i))// if there is same
                 return true; 
         return false;
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: