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 ...
随机推荐
- 找回误删除的UBUNTU16.04桌面壁纸图片,或把桌面背景图片另存。20170114
今天遇到一个小问题,之前下载并设置为桌面壁纸的一张图片在整理文件的时候不小心删除了.由于想不起来当时从哪里找到的图,所以就想把当前桌面壁纸重新保存.经网上查询,未见正确的保存方法,故写在此处备忘. 1 ...
- 代码块(Block)回调一般阐述
本章教程主要对代码块回调模式进行讲解,已经分析其他回调的各种优缺点和适合的使用场景. 代码块机制 Block变量类型 Block代码封装及调用 Block变量对普通变量作用域的影响 Block回调接口 ...
- spring No adapter for handler 错误
为了实现controller对JSON数据的自动解析,在spring-mvc.xml文件中增加了 <bean class="org.springframework.web.servle ...
- linux中ssh登录Permanently added (RSA) to the list of known hosts问题解决
文章出自http://www.2cto.com/os/201307/227199.html linux中ssh登录Permanently added (RSA) to the list of know ...
- 转:LoadRunner响应时间与用户体验时间不一致问题的深入分析
在新一代一期项目非功能测试过程中,我们发现了LoadRunner测试响应时间与客户端实际用户体验时间不一致的现象.例如员工渠道上线后,客户体验时间远远超过了LoadRunner测试响应时间.本文针对这 ...
- vb.net_一个半成品
Imports System.Text Imports System.Runtime.InteropServices Public Class Form1 '引用win32api进行枚举窗体句柄操作 ...
- 2016年团体程序设计天梯赛-决赛 L1-7. 到底是不是太胖了(10)
据说一个人的标准体重应该是其身高(单位:厘米)减去100.再乘以0.9所得到的公斤数.真实体重与标准体重误差在10%以内都是完美身材(即 |真实体重-标准体重| < 标准体重x10%).已知市斤 ...
- XML 树结构
XML 树结构 XML 文档形成了一种树结构,它从"根部"开始,然后扩展到"枝叶". 一个 XML 文档实例 XML 文档使用简单的具有自我描述性的语法: &l ...
- 图的存储结构:邻接矩阵(邻接表)&链式前向星
[概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...
- deepin2014.1安装搜狗后却找不到图标及配置
点开Input Method Configration; 点左下角添加输入法; 将Only Ohow Current Language前 的勾去掉,选择出现的搜狗输入法. FYI.