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 ...
随机推荐
- Error LNK2001 无法解析的外部符号 的几种情况及解决办法
最近遇到的关于VS里编译出现的“无法解析的外部符号”问题,在网上寻求解决办=办法时查到下面的博客内容,作者讲解的挺全面的,作为收藏以备将来查询. 原文http://blog.csdn.net/shen ...
- opencv 手写选择题阅卷 (一)表格设计与识别
(一)答题表格设计与识别 实际设计好的表格如下图 为了图像精确,表格和四角的标记都是由程序生成的,文字和数据是后期排版软件添加上去的. 图中四角的四个黑方块主要用来定位表格,然后就可以切割出每个单元格 ...
- 如何在Android SDK 下查看应用程序输出日志的方法
该文章源于安卓教程网(http://android.662p.com),转载时要注明文章的来自和地址,感谢你的支持. 在Android程序中可以使用 android.util.Log 类来 ...
- zabbix短信接口调用
#!/bin/bash TIME=`date +%Y-%m-%d` KEY="UJK9rk50HD8du8JE8h87RUor0KERo5jk" username="za ...
- C语言函数参数既做出参又做入参的代表
//使用fcntl对文件进行加锁 #include "stdio.h"#include "unistd.h"#include "fcntl.h&quo ...
- 第一个C++
输入:cin>>(相当于scanf) #include <iostream> using namespace std; int main() { int number; ...
- struts2中的常量
struts2中的常量: 在:struts2-core-2.1.8.1\org\apache\struts2\default.properties 文件里 <!-- 配制i18n国际化--> ...
- Microsoft.DirectX.DirectSound学习(一)
背景:为什么用到这个类库呢?公司要一个要播放音频文件(.wav)的功能,本来想着很ez的事,网上提供的jq插件.本地也有很多播放器,怎么用都行.可当我实现的时候发现大部分网上插件在火狐上不支持.wav ...
- 【winform】如何在DateTimePicker中显示时分秒
1. 首先属性里面的Format属性value设置为Custom(默认为Long) 2. 对应的Custom属性value设置为yyyy-MM-dd HH:mm:ss
- IAR:Error [Li005]:no definition for"***" 问题之连接
对于 IAR 出现的 Error[Li005] 链接错误,网上已经给出了比较详尽的解决方法,而对于这次记录,主要是记录解决问题的思路. 网上给出的方法:http://blog.csdn.net/yue ...