Question

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.

Solution 1 HashMap

Time complexity O(n), space cost O(n)

 public class Solution {
public boolean containsDuplicate(int[] nums) {
Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
int length = nums.length;
if (length <= 1)
return false;
for (int i = 0; i < length; i++) {
if (counts.containsKey(nums[i]))
return true;
else
counts.put(nums[i], 1);
}
return false;
}
}

Solution 2 Set

Time complexity O(n), space cost O(n)

 public class Solution {
public boolean containsDuplicate(int[] nums) {
int length = nums.length;
if (length <= 1)
return false;
Set<Integer> set = new HashSet<Integer>();
for (int i : nums) {
if (!set.add(i))
return true;
}
return false;
}
}

Contains Duplicate 解答的更多相关文章

  1. Find the Duplicate Number 解答

    Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...

  2. Contains Duplicate II 解答

    Question Given an array of integers and an integer k, find out whether there are two distinct indice ...

  3. OpenLDAP使用疑惑解答及使用Java完成LDAP身份认证

    导读 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的数据库系统,其专门针对读取,浏览 ...

  4. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. 【LeetCode题意分析&解答】39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  6. 【LeetCode题意分析&解答】33. Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  7. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  8. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  9. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

随机推荐

  1. The Hitchhiker’s Guide to Python! — The Hitchhiker's Guide to Python

    The Hitchhiker's Guide to Python! - The Hitchhiker's Guide to Python The Hitchhiker's Guide to Pytho ...

  2. 学习javascript基础知识系列第三节 - ()()用法

    总目录:通过一段代码学习javascript基础知识系列 注意: 为了便于执行和演示,建议使用chrome浏览器,按F12,然后按Esc(或手动选择)打开console,在console进行执行和演示 ...

  3. Unity 安卓下DLL热更新一(核心思想)

    大家都知道一谈起热更新的话首选是Ulua这个插件, 其实Unity可以使用dll热更新的,如果你实在不想用Lua来编写逻辑,0.0请下看Dll+AssetBundle如何实现热更新的.让你看完这个文章 ...

  4. PC-HTML5-搜索框

    代码如下: <input type="text" placeholder="输入 回车搜索" autofocus x-webkit-speech>很 ...

  5. Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.3

    Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.3 http://blog.csdn.net/sunbow0 第二章Deep ...

  6. php 依据字符串生成相应数组方法

    php 依据字符串生成相应数组方法 比如: <?php $config = array( 'project|page|index' => 'content', 'project|page| ...

  7. 智能路由——ESB

    SOA之我见 SOA已然是企业级开发的必定之路.有人会问:我们有了OOP,还须要SOA吗?好吧我承认,这个问题也困扰了我非常久.现现在我得出的结论是:OOP是OOP,SOA是SOA. OOP是指面向对 ...

  8. 《JavaScript 闯关记》之作用域和闭包

    作用域和闭包是 JavaScript 最重要的概念之一,想要进一步学习 JavaScript,就必须理解 JavaScript 作用域和闭包的工作原理. 作用域 任何程序设计语言都有作用域的概念,简单 ...

  9. Emit技术使用实例及应用思路

    System.Reflection.Emit提供了动态创建类并生成程序集的功能. 适用于.NET Framework 2.0及其以后的版本. 动态生成类在对于O/R Mapping来说有很大的作用,在 ...

  10. poj1201/zoj1508/hdu1384 Intervals(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Intervals Time Limit: 10 Seconds      Mem ...