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 ...
随机推荐
- Git 技巧小结
本篇博客内的内容,主要摘抄自 廖雪峰的 Git教程,这篇教程写的通俗易懂,步步深入,是我见过最棒的Git教程了.下面的全部内容,摘抄自此教程,有需要的朋友,请看完整版. Git版本库 git在创建版本 ...
- ubuntu 阿里云安全配置
1. 添加新用户, 加入 root 组, 赋予 sudo 权限 2. 禁用 root 3.
- [转]SET NOCOUNT ON
ref: http://www.cnblogs.com/jayleke/archive/2010/07/10/1774758.html 在存储过程,触发器中,经常用到SET NOCOUNT ON: 作 ...
- 【转】oracle中触发器中:new和:old 的使用方法
create or replace trigger TRI_PrintTest before delete or insert or update on TEST_EXAM --触发事件 for ea ...
- google map 点击获取经纬度
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ES6学习笔记(九)
1.概述 ES5的对象属性名都是字符串,这容易造成属性名的冲突.比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin模式),新方法的名字就有可能与现有方法产生冲突.如果有一种机制 ...
- jsp分页代码之pageUtil类
pageUtil类负责得到每页的开始数和结束数 package control; public class PageUtil { private int pageSize;//每页显示的条数 priv ...
- WPF多线程演示
WPF中的几种处理线程的工作方式: 1.简单的DispatcherTimer类似Timer控件 2.需要处理UI同步时,Dispatcher DispatcherOpertion 3.增强的Threa ...
- 计算器(delphi)
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Eclipse--Team--SVN--URL修改
1.团队开发服务器,有的时候会更换地址 解决: eclipse--菜单Windows-Show View-others-svn--svn资源库 打开资源库面板 右击http://localhost:9 ...