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.


题目标签:Array, Hash Table

  题目给了我们一个nums array, 让我们来检查它是否有重复的数字。遍历nums array, 如果hash set 没有这个数字,就存入;有的话,直接返回true。

Java Solution:

Runtime beats 50.21%

完成日期:05/26/2017

关键词:Array, Hash Table

关键点:利用Hash set 的特性,独一性

 public class Solution
{
public boolean containsDuplicate(int[] nums)
{
if(nums.length == 0 || nums == null)
return false; HashSet<Integer> unique = new HashSet<>(); for(int i=0; i<nums.length; i++)
{
if(unique.contains(nums[i]))
return true;
else
unique.add(nums[i]);
} return false;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 217. Contains Duplicate (包含重复项)的更多相关文章

  1. [LeetCode] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  2. LeetCode 219. Contains Duplicate II (包含重复项之二)

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  3. [LeetCode] Contains Duplicate 包含重复值

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  4. 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 ...

  5. LN : leetcode 217 Contains Duplicate

    lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...

  6. 25. leetcode 217. Contains Duplicate

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  7. LeetCode 217 Contains Duplicate 解题报告

    题目要求 Given an array of integers, find if the array contains any duplicates. Your function should ret ...

  8. leetcode 217 Contains Duplicate 数组中是否有重复的数字

     Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...

  9. LeetCode 217 Contains Duplicate

    Problem: Given an array of integers, find if the array contains any duplicates. Your function should ...

随机推荐

  1. sscanf和正则表达式

    sscanf() - 从一个字符串中读进与指定格式相符的数据.      函数原型: Int sscanf( string str, string fmt, mixed var1, mixed var ...

  2. Zookeeper-5分钟快速掌握分布式应用程序协调服

    一.Zookeeper 安装 1.zookeeper支持brew安装. ➜ ~ brew info zookeeper zookeeper: stable (bottled), HEAD Centra ...

  3. headfirst设计模式(3)—装饰者模式

    序 好久没写设计模式了,自从写了两篇之后,就放弃治疗了,主要还是工作太忙了啊(借口,都是借口),过完年以后一直填坑,填了好几个月,总算是稳定下来了,可以打打酱油了. 为什么又重新开始写设计模式呢?学习 ...

  4. JSP第六篇【自定义标签之传统标签】

    为什么要使用自定义标签? JSTL标签库只提供了简单的输出等功能,没有实现任何的HTML代码封装,并且某些复杂类型转换,或者逻辑处理的时候,JSTL标签库完成不了,需要自定义标签! 编写自定义标签的步 ...

  5. CVE-2016-10191 FFmpeg RTMP Heap Buffer Overflow 漏洞分析及利用

    作者:栈长@蚂蚁金服巴斯光年安全实验室 一.前言 FFmpeg是一个著名的处理音视频的开源项目,使用者众多.2016年末paulcher发现FFmpeg三个堆溢出漏洞分别为CVE-2016-10190 ...

  6. layer子层给父层页面元素赋值,以达到向父层页面传值的效果

    父层: jsp中: //页面上添加一个隐藏的输入框待用于被子层设置value,从而将子层的数据传递到此页面 <input type="hidden" id="get ...

  7. JAVA数据流再传递

    有一个filter类,在请求进入的时候读取了URL信息,并且读取了requestBod中的参数信息,那么在请求到达实际的控制层时,入参信息是拿不到的,对这种情况就需要数据流做再传递处理. 处理原理:使 ...

  8. String的replace和replaceAll

    replace(CharSequence target, CharSequence replacement) 这里CharSequence是一个接口 实现类包括CharBuffer, Segement ...

  9. iOS逆向环境以及常用命令行(逆向一)

    一.环境介绍 越狱环境:iPhone 5s iOS9.3.1 yueyu:~ root# uname -a Darwin yueyu 15.4.0 Darwin Kernel Version 15.4 ...

  10. End up with More Teams UVA - 11088

    End up with More Teams Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu ...