217. Contains Duplicate
题目:
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.
链接: http://leetcode.com/problems/contains-duplicate/
题解:
求数组中是否有重复元素。第一反应是用HashSet。 还可以用Bitmap来写。二刷要都补充上。
Time Complexity - O(n), Space Complexity - O(n)
public class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums == null || nums.length == 0)
return false;
Set<Integer> set = new HashSet<>();
for(int i : nums) {
if(set.contains(i))
return true;
else
set.add(i);
}
return false;
}
}
二刷:
也是跟1刷一样,使用一个Set来判断
Java:
Time Complexity - O(n), Space Complexity - O(n)
public class Solution {
public boolean containsDuplicate(int[] nums) {
if (nums == null || nums.length == 0) {
return false;
}
Set<Integer> set = new HashSet<>();
for (int i : nums) {
if (!set.add(i)) {
return true;
}
}
return false;
}
}
三刷:
直接使用set的话会超时,我们需要给Set设置一个初始值来避免resizing的过程。一般来说loading factor大约是0.75,最大的test case大约是30000个数字,所以我们用40000左右的容量的HashSet应该就足够了,这里我选的41000。
这道题也可以先排序再比较前后两个元素,那样的话是O(nlogn)时间复杂度,但可以做到O(1)空间复杂度。
Java:
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public boolean containsDuplicate(int[] nums) {
if (nums == null) {
return false;
}
Set<Integer> set = new HashSet<>(41000);
for (int i : nums) {
if (!set.add(i)) {
return true;
}
}
return false;
}
}
Update:
再写却并没有碰到超时的情况。
public class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>(nums.length);
for (int num : nums) {
if (!set.add(num)) return true;
}
return false;
}
}
Reference:
https://leetcode.com/discuss/70186/88%25-and-99%25-java-solutions-with-custom-hashing
https://leetcode.com/discuss/59208/20ms-c-use-bitmap
https://leetcode.com/discuss/37219/possible-solutions
https://leetcode.com/discuss/37190/java-o-n-ac-solutions-with-hashset-and-bitset
217. Contains Duplicate的更多相关文章
- 217. Contains Duplicate(C++)
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- 25. leetcode 217. Contains Duplicate
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array
后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...
- 217. Contains Duplicate【easy】
217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...
- LN : leetcode 217 Contains Duplicate
lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...
- leetcode 217 Contains Duplicate 数组中是否有重复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- 【LeetCode】217. Contains Duplicate (2 solutions)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 217. Contains Duplicate@python
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- leetcode 217 Contains Duplicate 数组中是否有反复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
随机推荐
- Contest1065 - 第四届“图灵杯”NEUQ-ACM程序设计竞赛(个人赛)A蔡老板的会议
题目描述 图灵杯个人赛就要开始了,蔡老板召集俱乐部各部门的部长开会.综合楼有N (1<=N<=1000)间办公室,编号1~N每个办公室有一个部长在工(mo)作(yu),其中X号是蔡老板的办 ...
- 类似QQ的应用毗邻(Pilin)即时聊天源码
这个应用是从安卓教程网分享过了的,个人觉得这个还是挺不错的,毗邻(Pilin)即时聊天应用源码,承诺的 基于xmpp openfire asmack 的即时聊天应用,继续完善,现在只完成了文字.表 ...
- PHP 反射 ReflectionClass
今天遇到了这样一个问题,如下代码: classA.php <?php class ClassA{ public function funcAa(){ } public function func ...
- border-radius导致overflow:hidden失效问题。
如果一个父元素设置了overflow:hidden属于的同时还设置了border-radius属性,那么如果想隐藏超出的子元素,四个圆角处会出现超出圆角依然显示的bug: 一种方法是运用-webkit ...
- 关于canvas中的jquery
关于h5,相比前端的同事们都很了解了吧!h5里面有个canvas,现在用的蛮火.但是canvas里面的代码确实是有点繁多,特别是要对于图形做什么操作的时候...我昨天无意间发现了一个canvas的插件 ...
- AjaxFileUpload Firefox 不工作异常 (zero-width space characters from a JavaScript string)
Firefox 返回的提示报错parse error (Chrome 和 IE正常) 打印出来返回的字符串,目测正常 将字符串放入notepad++, 转换字符集为ANSI 发现多出了欧元符号 通过j ...
- Hadoop学习---安装部署
hadoop框架 Hadoop使用主/从(Master/Slave)架构,主要角色有NameNode,DataNode,secondary NameNode,JobTracker,TaskTracke ...
- EXTJS 4.2 资料 控件textfield中fieldLabel去掉冒号,控件label的长度
代码: labelSeparator: '', // 去掉laebl中的冒号 labelWidth: 10,//控件label的长度
- ISoft(开源)专用下载器
继 两年的坚持,最后还是决定将ISoft开源 之后,今天再共享一款ISoft专用下载器小工具.这款工具是一年前开发的,也是一直闲置着没去扩展更新.当时开发出来就是仿穿越火线专用下载器的样式来做的,现在 ...
- Sharepoint delegate control
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://sch ...