Contains Duplicate 解答
Question
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.
Solution 1 HashMap
Time complexity O(n), space cost O(n)
public class Solution {
public boolean containsDuplicate(int[] nums) {
Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
int length = nums.length;
if (length <= 1)
return false;
for (int i = 0; i < length; i++) {
if (counts.containsKey(nums[i]))
return true;
else
counts.put(nums[i], 1);
}
return false;
}
}
Solution 2 Set
Time complexity O(n), space cost O(n)
public class Solution {
public boolean containsDuplicate(int[] nums) {
int length = nums.length;
if (length <= 1)
return false;
Set<Integer> set = new HashSet<Integer>();
for (int i : nums) {
if (!set.add(i))
return true;
}
return false;
}
}
Contains Duplicate 解答的更多相关文章
- Find the Duplicate Number 解答
Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...
- Contains Duplicate II 解答
Question Given an array of integers and an integer k, find out whether there are two distinct indice ...
- OpenLDAP使用疑惑解答及使用Java完成LDAP身份认证
导读 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的数据库系统,其专门针对读取,浏览 ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode题意分析&解答】33. Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
随机推荐
- 解决Struts2.2.20版本的标签不支持style属性的问题
我先把Exception错误信息贴出来:org.apache.jasper.JasperException: /WEB-INF/jsp/topicAction/addUI.jsp (line: 40, ...
- 3Sum Smaller 解答
Question Given an array of n integers nums and a target, find the number of index triplets i, j, k w ...
- Codeforces243C-Colorado Potato Beetle(离散化+bfs)
Old MacDonald has a farm and a large potato field, (1010 + 1) × (1010 + 1) square meters in size. Th ...
- 04747_Java语言程序设计(一)_第10章_网络与数据库编程基础
例10.1说明InetAddress类的用法的应用程序. public class Example10_1 { public static void main(String args[]) { try ...
- 第01讲- Android背景知识
第01讲Android背景知识 Android是基于Linux系统 Android系统框图 : 第一.操作系统层(OS) 第二.各种库(Libraries)和Android 运行环境(RunTime) ...
- ios delegate 和 block
//委托的协议定义 @protocol UpdateDelegate <NSObject> - (void)update; @end @interface Test : NSObject ...
- 格而知之15:我所理解的Block(1)
1.Block 本质上是一个struct结构体,在这个结构体中,最重要的成员是一个函数(当然除函数外还有其他重要的成员). 2.在开始解析Block之前,首先来回顾一下Block的格式.Block相关 ...
- [转]Laravel 4之验证
Laravel 4之验证 http://dingjiannan.com/2013/laravel-validation/ 基本验证 使用Validator::make($data, $rules)验证 ...
- [置顶] 正则表达式应用:匹配IP地址
都知道iP地址有四个数值,三个点号组成.三个数值的具体范围为0到255,为了使用正则表达式匹配就必须分析IP地址的组成 1先分析数值,2再组合数值和点号 1先分析数值 IP地址的数字范围从0到255, ...
- Flashback version/Transaction Query,FlashbackTable
Flashback version Query相对于Flashback Query 只能看到某一点的对象状态, Oracle 10g引入的Flashback Version Query可以看到过去某个 ...