217. Contains Duplicate

Easy

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.

Example 1:

Input: [1,2,3,1]
Output: true

Example 2:

Input: [1,2,3,4]
Output: false

Example 3:

Input: [1,1,1,3,3,4,3,2,4,2]
Output: true
package leetcode.easy;

public class ContainsDuplicate {
public boolean containsDuplicate1(int[] nums) {
for (int i = 0; i < nums.length; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[j] == nums[i]) {
return true;
}
}
}
return false;
}
// Time Limit Exceeded public boolean containsDuplicate2(int[] nums) {
java.util.Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
} public boolean containsDuplicate3(int[] nums) {
java.util.Set<Integer> set = new java.util.HashSet<>(nums.length);
for (int x : nums) {
if (set.contains(x)) {
return true;
} else {
set.add(x);
}
}
return false;
} @org.junit.Test
public void test1() {
int[] nums1 = { 1, 2, 3, 1 };
int[] nums2 = { 1, 2, 3, 4 };
int[] nums3 = { 1, 1, 1, 3, 3, 4, 3, 2, 4, 2 };
System.out.println(containsDuplicate1(nums1));
System.out.println(containsDuplicate1(nums2));
System.out.println(containsDuplicate1(nums3));
} @org.junit.Test
public void test2() {
int[] nums1 = { 1, 2, 3, 1 };
int[] nums2 = { 1, 2, 3, 4 };
int[] nums3 = { 1, 1, 1, 3, 3, 4, 3, 2, 4, 2 };
System.out.println(containsDuplicate2(nums1));
System.out.println(containsDuplicate2(nums2));
System.out.println(containsDuplicate2(nums3));
} @org.junit.Test
public void test3() {
int[] nums1 = { 1, 2, 3, 1 };
int[] nums2 = { 1, 2, 3, 4 };
int[] nums3 = { 1, 1, 1, 3, 3, 4, 3, 2, 4, 2 };
System.out.println(containsDuplicate3(nums1));
System.out.println(containsDuplicate3(nums2));
System.out.println(containsDuplicate3(nums3));
}
}

LeetCode_217. Contains Duplicate的更多相关文章

  1. 代码的坏味道(14)——重复代码(Duplicate Code)

    坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...

  2. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

  3. iOS开发 引用第三方库出现duplicate symbol时的处理方法

      该篇文章是我自己从我的新浪博客上摘抄过来的, 原文链接为: http://blog.sina.com.cn/s/blog_dcc636350102wat5.html     在iOS开发中, 难免 ...

  4. C语言调试过程中duplicate symbol错误分析

    说明:在我们调试C语言的过程中,经常会遇到duplicate symbol错误(在Mac平台下利用Xcode集成开发环境).如下图: 一.简单分析一下C语言程序的开发步骤. 由上图我们可以看出C语言由 ...

  5. Duplicate entry 'javajavajav' for key 'username'

    org.apache.ibatis.exceptions.PersistenceException: ### Error updating database.  Cause: com.mysql.jd ...

  6. [LeetCode] Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  7. [LeetCode] Find the Duplicate Number 寻找重复数

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  8. [LeetCode] 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] Contains Duplicate II 包含重复值之二

    Given an array of integers and an integer k, return true if and only if there are two distinct indic ...

随机推荐

  1. python为什么不需要重载函数

    https://www.cnblogs.com/erbaodabao0611/p/7490439.html

  2. 08 node.js 的使用

    创建包 目录结构 cmd   cd 到当前目录:  \ 执行 npm init //创建一个包 1 2. 3. 4.包的安装 npm install jquery --save npm install ...

  3. Spring入门(四)——整合Mybatis

    1. 准备jar包及目录结构 2. 配置db.properties driver = com.mysql.jdbc.Driver url = jdbc:mysql://127.0.0.1:3306/H ...

  4. npm 安装全局包 不是内部或外部命令的问题

    场景: npm已经安装成功  ,通过npm install -g 安装的 全局包 提示不是内部或外部命令 第一步: npm list -g --depth=0:查看npm全局包的路径,和有哪些安装包 ...

  5. Open Judge 1.4 09

    09:判断能否被3,5,7整除 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个整数,判断它能否被3,5,7整除,并输出以下信息:1.能同时被3,5,7整除(直接输出3 5 ...

  6. P4921 【情侣?给我烧了!】

    加强前这道题还是比较友好的 首先我们设\(g_x\)为x对情侣没有一对坐在一起的数量 然后答案就可以表示成:\(C_n^k*A_n^k*2^k*g_{n-k}\) 这里的复杂度是\(O(T*N)\), ...

  7. SQL优化(子文章)(持续更新)

    -----> 总文章 入口 文章目录 [-----> 总文章 入口](https://blog.csdn.net/qq_37214567/article/details/90174445) ...

  8. 移动端滚动选择器mobileSelect.js

    一款多功能的移动端滚动选择器,支持单选到多选.支持多级级联.提供自定义回调函数.提供update函数二次渲染.重定位函数.兼容pc端拖拽等等.. 特性 原生js移动端选择控件,不依赖任何库 可传入普通 ...

  9. 美团Android自动化之旅—适配渠道包

    http://tech.meituan.com/mt-apk-adaptation.html 概述 前一篇文章(美团Android自动化之旅-生成渠道包)介绍了Android中几种生成渠道包的方式,基 ...

  10. SyntaxError: Non-ASCII character 'æ' in file csdn.py on line 7, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

    错误信息: SyntaxError: Non-ASCII character , but no encoding declared; see http://python.org/dev/peps/pe ...