LeetCode OJ 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.
这个问题比较简单,判断一个数组里的是否有重复出现的数字。代码简单如下:
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的更多相关文章
- 【一天一道LeetCode】#217. Contains Duplicate
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】217. Contains Duplicate (2 solutions)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 【LeetCode】217. Contains Duplicate 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计词频 使用set 排序 日期 [LeetCo ...
- LeetCode【217. Contains Duplicate】
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 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 ...
- 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 ...
- 【LeetCode】217. Contains Duplicate
题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...
- 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 ...
- LeetCode OJ:Contains Duplicate(是否包含重复)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- 类加载class loader
Class装载验证流程: 加载:取得类的二进制流,转为方法区的数据结构,在java堆中生成对应的java.lang.class对象 链接:就是将已经读入到内存的类的二进制数据合并到虚拟机的运行时环境中 ...
- MVC修改文件上传大小问题
在web.config文件的system.web节点的httpRuntime节点加入 maxRequestLength="2147483647" executionTimeout= ...
- HDU - 2290 Find the Path(最短路)
HDU - 2290 Find the Path Time Limit: 5000MS Memory Limit: 64768KB 64bit IO Format: %I64d & % ...
- RPD添加网址、变量
- 给hexo添加评论系统
默认主题 landscape 文件目录,comments为新建的 _config.yml layout -- _partial -- article.ejs |- comments -- disqus ...
- javaWEB总结(9):自定义HttpServlet
前言:我们知道 MyHttpServlet是MyGenericServlet的子类,MyHttpServlet会继承父类的方法,可是却很少去追问MyHttpServlet中的doGet方法和doPos ...
- OGG中断后,重新同步操作
模拟一下goldengata中断后,重新同步操作: 1.关掉源端抽取进程 GGSCI (20081122-2105) 15> info all Program Status Group Lag ...
- Java多线程:线程同步与关键字synchronized
一.同步的特性1. 不必同步类中所有的方法, 类可以同时拥有同步和非同步方法.2. 如果线程拥有同步和非同步方法, 则非同步方法可以被多个线程自由访问而不受锁的限制. 参见实验1:http://blo ...
- noip2015Day2T1-跳石头
题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有N ...
- 《JS权威指南学习总结--6.6属性getter和setter》
内容要点: 一.对象属性 对象属性是由名字.值和一组特性构成的.在ES5中,属性值可以用一个或两个方法替代,这两个方法就是getter和setter.由getter和setter定义的属性称做 ...