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. QGraphicsView,QGraphicsScene,QGraphicsItem

    参考:Qt4 开发实践第八章 图形视图QGraphicsView #ifndef DRIVEDGRAPH_H #define DRIVEDGRAPH_H #include <QObject> ...

  2. 删除tppabs,href="javascript:if(confirm)...",、/*tpa=http://...

    扒网站,据说是web从业人员的必备技能; 废话不多,下面应该是你想要的; 1:   tppabs="h[^"]*" 2: href="javascript\:i ...

  3. OPCode详解及汇编与反汇编原理

    1. 何为OPCode 在计算机科学领域中,操作码(Operation Code, OPCode)被用于描述机器语言指令中,指定要执行某种操作的那部分机器码,构成OPCode的指令格式和规范由处理器的 ...

  4. php 简单使用redis 队列示例

    public function redisAction(){ $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo &qu ...

  5. PHP利用执行操作符;模拟shell命令

    $out = `ls -la`; echo '<pre>'.$out.'</pre>'; 输出结果: total 10 drwxrwxrwx 1 root root 4096 ...

  6. maven ssm 编译异常记录:

    maven ssm 编译异常记录: javax.servlet.jsp 解决: 清除 tomacat libraries 修改 pom 文件 <dependency> <groupI ...

  7. [提权]sudo提权复现(CVE-2019-14287)

    2019年10月14日, sudo 官方在发布了 CVE-2019-14287 的漏洞预警. 0x00 简介 sudo 是所有 unix操作系统(BSD, MacOS, GNU/Linux) 基本集成 ...

  8. dbutils的环境搭建以及使用

    DBUtils的简单实现方式 第一步.创建Java工程[此处使用的是maven工程]并导入jar包 <!--导入mysql数据库驱动--> <dependency> <g ...

  9. Spring mybatis thymeleaf 基础操作,实现数据展示,修改,删除,查询

    目录结构如图 index.html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thyme ...

  10. 【多线程与并发】:Java中的锁

    锁的概念 锁是用来控制多个线程访问共享资源的方式,一般来说,一个锁可以防止多个线程同时访问共享资源(但有些锁可以允许多个线程并发的访问共享资源,如读写锁). 在JDK1.5之前,Java是通过sync ...