Leetcode: 217. Contains Duplicate(数组是否包含重复数字)

来源:互联网 发布:mac软件下载网站排名 编辑:程序博客网 时间:2024/06/08 14:01

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.

其实这个题我以前做过类似的,基于HashSet可以很方便的求解;
代码如下:

    public static boolean containsDuplicate(int[] nums){        if (nums.length==0||nums.length==1) {            return false;        }        HashSet<Integer> hashSet = new HashSet<Integer>();        for (int i = 0; i < nums.length; i++) {            if(hashSet.add(nums[i])==false)                return true;        }        return false;    }
阅读全文
0 0