[LeetCode] Contains Duplicate
2015-08-13 14:32
316 查看
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就很容易做了。用hashset中的add method。因为add只能add在hashset中没有的元素。所以同一个元素不可能被add成功第二次。
代码如下。(loop那里可以用enhanced loop再简化下,不过我懒得改了)
这道题用hashset就很容易做了。用hashset中的add method。因为add只能add在hashset中没有的元素。所以同一个元素不可能被add成功第二次。
代码如下。(loop那里可以用enhanced loop再简化下,不过我懒得改了)
public class Solution { public boolean containsDuplicate(int[] nums) { if(nums==null||nums.length==0){ return false; } HashSet<Integer> test=new HashSet<Integer>(); for(int i=0;i<nums.length;i++){ if(!test.add(nums[i])){ return true; } } return false; } }
相关文章推荐
- textview constrain count
- AFNetworkingErrorDomain 错误解决方法
- Redmine的创建任务的性能问题
- http://zhidao.baidu.com/link?url=fX6C1xFLsqiuTY88cjwJYal2x52rOwlJstmz7KWyMc6l9j3FHw2yhvp83timZ86pwhqQ8rONj2xkgo2wbU2tLK
- hdu 1021 Fibonacci Again
- Pywinauto在Windows Twain Driver自动化测试中的应用研究
- RAID0,RAID1,RAID10,RAID5各需几块盘才可组建
- Windows 经典的宏——CONTAINING_RECORD
- [USACO Training]二进制数01串
- hdu 1021 Fibonacci Again
- POJ 2527 Polynomial Remains 多项式运算
- poj 1273 Drainage Ditches(最大流)
- AI:确定性AI
- Climbing Stairs
- [CodeForces 300D Painting Square]DP
- zoj 3175 Number of Containers 分块加速
- hdu1848 Fibonacci again and again
- 运项目难点之ScrollView中嵌套百度地图(BaiduMap)的解决方案
- C - ACboy needs your help again!
- 利用Baidu API正向和反向解析地址