LeetCode_217. Contains Duplicate
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.
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的更多相关文章
- 代码的坏味道(14)——重复代码(Duplicate Code)
坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- iOS开发 引用第三方库出现duplicate symbol时的处理方法
该篇文章是我自己从我的新浪博客上摘抄过来的, 原文链接为: http://blog.sina.com.cn/s/blog_dcc636350102wat5.html 在iOS开发中, 难免 ...
- C语言调试过程中duplicate symbol错误分析
说明:在我们调试C语言的过程中,经常会遇到duplicate symbol错误(在Mac平台下利用Xcode集成开发环境).如下图: 一.简单分析一下C语言程序的开发步骤. 由上图我们可以看出C语言由 ...
- Duplicate entry 'javajavajav' for key 'username'
org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: com.mysql.jd ...
- [LeetCode] Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
随机推荐
- LightOJ - 1410 - Consistent Verdicts(规律)
链接: https://vjudge.net/problem/LightOJ-1410 题意: In a 2D plane N persons are standing and each of the ...
- [NgRx 8] Basic of NgRx8
1. First step is creating action creator Action name should be clear which page, which functionality ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
- .net文件夹上传下载组件
ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...
- 开始编写Makefile(三)Makefile的默认模式规则
1.make中建立的其他语言的规则 SCCS , RCS , ar, lex 和yacc 命令 2.为了建立一个目标,make会遍历一连串的依赖关系:这个是为 决定何处开始创建: 如果没有找到目标文 ...
- 关灯问题II 状压DP
关灯问题II 状压DP \(n\)个灯,\(m\)个按钮,每个按钮都会对每个灯有不同影响,问最少多少次使灯熄完. \(n\le 10,m\le 100\) 状压DP的好题,体现了状压的基本套路与二进制 ...
- shell编程题(四)
编译当前目录下的所有.c文件 #!/bin/bash ] ;] 输入参数个数 echo "Please follow up file.c!" echo "eg: ./ma ...
- 数据结构实验之查找二:平衡二叉树 (SDUT 3374)
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; ...
- 贪心算法训练(四)——(HDU1050)Moving Tables
题目描述 在一个狭窄的走廊里将桌子从一个房间移动到另一个房间,走廊的宽度只能允许一个桌子通过.给出 t,表示有 t 组测试数据,再给出 n,表示要移动 n 个桌子.n 下面有 n 行,每行两个数字,表 ...
- 网格布局 grid
推荐阅读:http://www.ruanyifeng.com/blog/2019/03/grid-layout-tutorial.html 1. 启动网格布局 div { display: grid; ...