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. log4j.properties全配置 (转)

    ###############################log4j.properties############################### ##### Global Log Leve ...

  2. IIS下访问网络驱动器(网络位置)

    System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.ex ...

  3. CSU 1639 队长,我想进集训队!

    水题 #include<cstdio> int main() { int x1, x2, x3, u, h; int n; while (~scanf("%d", &a ...

  4. iosTableView 局部全部刷新以及删除编辑操作

    局部刷新方法 添加数据 NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath index ...

  5. PAT 团体程序设计天梯赛-练习集L1-011. A-B

    本题要求你计算A-B.不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A-B. 输入格式: 输入在2行中先后给出字符串A和B.两字符串的长度 ...

  6. maven项目构建

    Maven是apache的一个开源项目.是一个用来把源代码构建成可发布的构件的工具. Maven的功能非常强大,可以认为是一个项目管理工具,不仅仅是一个构建工具. Maven本身的核心很小,但是可以在 ...

  7. POJ 1323 Game Prediction#贪心

    (- ̄▽ ̄)-* //既然是求最少能胜几次 //说明对方是要尽可能让我输 //但为了避免浪费,对方会用比我的牌大的牌中的最小pip的牌来击败我 #include<iostream> #in ...

  8. a标签href跳转---传值---禁止单引号

    禁止加单引号1  href传参的值  (&t_type='航班') 错误 <a href="check_apply?applyid=<?=$applyid?>&am ...

  9. 关于R语言的一些编程经验

    一个晚上写出一个能用的程序…… 来说说遇见的问题吧 zqw<-read.table(file = "c:/data/zqw.txt") zqw<-data.frame( ...

  10. WCF、Web API、WCF REST、Web Service的区别

    Difference between WCF and Web API and WCF REST and Web Service   The .Net framework has a number of ...