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. 流媒体知识 wiki

    媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等.最近因为项目的关系,需要理清媒 ...

  2. Error Codes Messages查找工具介绍

    当我们通过各种手段获取到一个Windows错误码后,如何获取对应的错误消息呢?有两种方法:一是用编程的手段(FormatMessage):其二是用现成的工具.第一种方法一般在我们编码的时候有用,而更多 ...

  3. Java获取类方法上的注解

    , fullName.indexOf("$$")); try { clz = Class.forName(fullName); } catch (ClassNotFoundExce ...

  4. C++语言第一课的学习

    // HelloApp.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include ...

  5. 讲解SQL数据库语句

    前言 大家好,我是 Vic,今天给大家带来讲解SQL数据库语句的概述,希望你们喜欢 数据库语句 create database teach; use teach; create table `teac ...

  6. Sage Math中的语法

    1.赋值后不能立即输出,而需要停顿.x= 3  不能输出显示,而 x= 3; x 可以显示. 2.可以用分号连续书写多行. 3.矩阵可以用 mtx[i, j]引用,但是行列号通常从0开始,维度n, m ...

  7. 模板 - 数据结构 - 线段树/SegmentTree

    区间求加法和: 单点修改的,普通线段树. struct SegmentTree { #define ls (o<<1) #define rs (o<<1|1) static c ...

  8. springcloud config自动刷新中文乱码问题

    摘录一段网上盛传的,如下: 在使用 spring cloud config 时,如果在 properties 文件里面有中文的话,会出现乱码. 乱码的原因是:spring 默认使用org.spring ...

  9. 【JVM】虚拟机类加载机制

    什么是类加载 虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. <[JVM]类文件结构& ...

  10. 第07组 Alpha冲刺(2/6)

    队长:摇光 队长:杨明哲 组长博客:求戳 作业博客:求再戳 队长:杨明哲 过去两天完成了哪些任务 文字/口头描述:重写后端,完成了数据请求部分的后端. 展示GitHub当日代码/文档签入记录:(组内共 ...