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 ...
随机推荐
- 第一节 Hibernate 基本配置
1 新建maven工程 1)打开eclipse,依次点击File---->New---->Maven Project. 2)选择org.apache.maven.archetypes ma ...
- python pil 安装
Ubuntu下 sudo pip install pil 安装PIL可能会出现问题,例如安装完成时显示JPEG support not available 或者 ZLIB (PNG/ZIP) supp ...
- js 中的正则表达式
一:正则表达式 定义:记录文本规则的代码 作用:表单验证,爬虫技术,可以对目标的内容进行替换. 二:正则表达式的组成 1:普通字符组成正则 浏览器的输出 2:定义字符集组成正则 3:特殊字符集组成正则 ...
- [Accessibility] Missing contentDescription attribute on image [可取行]失踪contentDescription属性图像
问题描述: [Accessibility] Missing contentDescription attribute on image [可取行]失踪contentDescription属性图像 原因 ...
- CentOS 6.4 中安装部署 Nutch 1.7
1.配置SSH 自行查阅相关资料 2.安装JDK,配置Java环境 自行查阅相关资料 3.安装SVN [root@master ~]# yum install -y subversion 通过SVN签 ...
- DB2分区表删除和添加分区
1.数据库版本 2.具体procedure DROP PROCEDURE DB2USER.TOOLS_PARTITION_TABLE_SHOW (VARCHAR ()); )) /********** ...
- 虚拟机安装Centos6.5之后的网络配置
我使用的是minimal模式安装的,默认是无法联网的,需要自己配置,下面我列举2种联网的配置方法 方法1: 默认使用的是NAT模式,修改/etc/sysconfig/network-scripts/i ...
- ISBN
问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位 ...
- Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey
Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原因有几种: 1.mapper.xml中没有加入name ...
- EF4.1之复杂类型
首先我们生成两张对应表: public class Client { public int ClientID { set; get; } public string ClientName { set; ...