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.

这个问题比较简单,判断一个数组里的是否有重复出现的数字。代码简单如下:

 public class Solution {
public boolean containsDuplicate(int[] nums) {
Map<Integer,Integer> map = new HashMap<>(0); for(int num : nums){
if(map.containsKey(num)) return true;
map.put(num,0);
}
return false;
}
}
 public class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums == null || nums.length <= 1) return false; Set<Integer> set = new HashSet<>();
for(int num : nums) {
if(!set.add(num)) return true;
}
return false;
}
}

LeetCode OJ 217.Contains Duplicate的更多相关文章

  1. 【一天一道LeetCode】#217. Contains Duplicate

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. 【LeetCode】217. Contains Duplicate (2 solutions)

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

  3. 【LeetCode】217. Contains Duplicate 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计词频 使用set 排序 日期 [LeetCo ...

  4. LeetCode【217. Contains Duplicate】

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

  5. LeetCode OJ 220.Contains Duplicate 3

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  6. LeetCode OJ 219.Contains Duplicate 2

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

  7. 【LeetCode】217. Contains Duplicate

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

  8. LeetCode OJ:Contains Duplicate III(是否包含重复)

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  9. LeetCode OJ:Contains Duplicate(是否包含重复)

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

随机推荐

  1. String与StringBuilder区别总结

    String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...

  2. 修改MANIFEST.MF方法

    步骤: 1.用winrar等其他解压工具,右键选择"用winrar打开": 2.找到MANIFEST.MF文件,鼠标左键拖拽到桌面: 3.最后一行加入Main-Class:(空格) ...

  3. crontab格式使用方式

    第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 下面是crontab的格式:分 时 日 月 星期 要运行的命令 这 ...

  4. express学习点滴- 永远不要忘记异步

    直接上两段代码,因为nodejs基于异步和事件回调的解决方式,涉及到异步的时候,问题往往藏得很深,以下这个简单的问题困扰了很久.之前怀疑是各种问题,到处改.直到最后一步一步跟代码,跟操作数据库部分豁然 ...

  5. css--多重样式

    1.一个DIV中既有class又有id <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  6. 5、Struts2自定义拦截器

    一.拦截器相关知识 1.Struts2框架剖析 Holly版本生活案例: 影视公司(拍电影)    ActionMapper 传媒公司(包装明星) ActionMapping 明星           ...

  7. ElasticSearch(3)-原理

    参考文档: http://learnes.net/distributed_crud/bulk_requests.html 一.分布式集群 1.1 空集群 单台机器,其中没有数据,也没有索引. 集群中一 ...

  8. C# 语言规范_版本5.0 (第0章 目录)

    C# 语言规范 版本5.0 注意 © 1999-2012 Microsoft Corporation.保留所有权利. Microsoft.Windows.Visual Basic.Visual C# ...

  9. Sql CE 数据库编程

    用户工具: http://www.linqpad.net/ 查询编辑数据 http://sqlcequery.codeplex.com/  查询编辑数据 开发人员工具: 安装:EF Tools nug ...

  10. HTTPS科普扫盲帖【转】

    为什么需要https HTTP是明文传输的,也就意味着,介于发送端.接收端中间的任意节点都可以知道你们传输的内容是什么.这些节点可能是路由器.代理等. 举个最常见的例子,用户登陆.用户输入账号,密码, ...