Leetcode Contains Duplicate
2015-09-22 00:53
597 查看
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 也有不同的用法。写了两种。
Java code:
或者:
解题思路:
判断array里是否有重复,就想到一种不能有重复的数据结构set, 如果已经有了这个值,则return true, 否则就加进这个set.当然,set 也有不同的用法。写了两种。
Java code:
public boolean containsDuplicate(int[] nums) { Set<Integer> values = new HashSet<Integer>(); for(int i=0; i< nums.length; i++) { if(values.contains(nums[i])){ return true; } values.add(nums[i]); } return false; }
或者:
public boolean containsDuplicate(int[] nums) { Set<Integer> values = new HashSet<Integer>(); for(int i=0; i< nums.length; i++) { if(!values.add(nums[i])){ return true; } } return false; }
相关文章推荐
- main()参数int argc,int *argv[]的学习
- Light oj 1086 - Jogging Trails(状压dp 将图变成欧拉图)
- Sublime Text 2 - There are no packages available for installation 解决方法(亲测可行)
- E: 无法获取锁 /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)解决方法
- exit()和_exit()函数 http://www.cnblogs.com/hnrainll/archive/2011/08/17/2142001.html
- Codeforces__Raising Bacteria
- Codeforces__Raising Bacteria
- xCode 7 编译出现错误:does not contain bitcode 解决办法
- 【转】友好的 ContainerView & AutoLayout 资料推荐
- [置顶] Linux DM9000网卡驱动程序完全分析 http://blog.csdn.net/ypoflyer/article/details/6209922
- AI-Tank
- 查询表锁争用情况 table_locks_waited & table_locks_immedaite
- setContentView(R.Layout.main)的意思
- raid5实现原理
- RAID5服务器磁盘阵列,两块盘坏了数据恢复
- http://blog.csdn.net/abcjennifer/article/details/7716281 过拟合问题
- 为什么使用HttpUrlConnection时候,用available方法不准确
- failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hiber
- shell浅谈之九子shell与进程处理 http://blog.csdn.net/taiyang1987912/article/details/39529291
- 23种设计模式之一(创建型模式)Factory模式 http://blog.csdn.net/taiyang1987912/article/details/43148913