【leetcode】217. Contains Duplicate
2016-06-20 12:33
471 查看
题目要求:
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 class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<Integer>();
for(int i=0;i<nums.length;i++)
{
if(!set.add(nums[i]))
{
return true;
}
}
return false;
}
}
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 class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<Integer>();
for(int i=0;i<nums.length;i++)
{
if(!set.add(nums[i]))
{
return true;
}
}
return false;
}
}
相关文章推荐
- raid0+磁盘加密
- DevExpress Grid中实现点击Detail获得Master rowHandle的方法
- Rails 5 有什么新特性?
- software RAID0+cryptsetup磁盘加密
- Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别
- 解决maillog文件删除后,空间不释放
- 机器学习之(二)OpenAI 生成模型聚焦无监督学习
- 文本查看命令 - head和tail
- QWidget QMainWindow QDialog 之间的区别
- flask-mail常见的邮箱配置问题解决
- 手把手做 raid0()
- http://cometdaily.com/
- TurboMail邮件系统容灾备份方案
- RHEL 7 RAID 5 (软)实验 磁盘阵列
- [转]go中的main函数和init函数
- hotmail收邮件-源码联系我~
- CONTAINING_RECORD
- http://blog.csdn.net/cywosp/article/details/32325449
- logwatch配置笔记
- Rails on Ubuntu常见问题及解决方法