LintCode 1320

包含重复值

题目描述
给定一个整数数组,查找数组是否包含任何重复项。如果数组中某个值至少出现两次,则函数应返回true,如果每个元素都是不同的,则返回false。

扫码免费做题
↓↓↓

样例 1
输入:nums = [1, 1]输出:True
样例 2
输入:nums = [1, 2, 3]输出:False
解题思路
使用HashSet判断重复
源代码
publicclassSolution{/** * @param nums: the given array * @return: if any value appears at least twice in the array */publicbooleancontainsDuplicate(int[] nums){// Write your code here Set<Integer> hashSet = new HashSet<>();for (int i = 0; i < nums.length; i ++) {if (!hashSet.add(nums[i])){returntrue; } }returnfalse; }}
查看完整代码,向左滑动
点击【阅读原文】,查看领扣原题
继续阅读
阅读原文