leetcode面试准备:Contains Duplicate I && II
1 题目
Contains Duplicate I
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.
接口: public boolean containsDuplicate(int[] nums)
Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that
nums[i] = nums[j]and the difference between i and j is at most k.
接口:public boolean containsNearbyDuplicate(int[] nums, int k)
2 思路
I:判断数组中有无重复元素。用HashTable的思想,实现用HashSet来做。
复杂度: Time:O(n);Space:O(n)
II : 在I的基础上,判断重复的元素相距离的位置是否在k范围之内。用HashTable的思想,实现用HashMap来做,<key, value> = <数组值,下标index>
复杂度: Time:O(n);Space:O(n)
注意:如何更新Map的值?考虑一下注意的测试用例 {1,0,1,1},1
3 代码
I
public boolean containsDuplicate(int[] nums) {
int len = nums.length;
Set<Integer> set = new HashSet<Integer>(len);
for (int num : nums) {
if (set.contains(num)) {
return true;
} else {
set.add(num);
}
}
return false;
}
II
public boolean containsNearbyDuplicate(int[] nums, int k) {
int len = nums.length;
Map<Integer, Integer> map = new HashMap<Integer, Integer>(len);
for (int i = 0; i < len; i++) {
if (map.containsKey(nums[i])) {
int diff = i - map.get(nums[i]);
if (diff <= k)
return true;
}
map.put(nums[i], i); // 不管有没有这个num,都要更新Map的值。为了通过这样的测试用例:{1,0,1,1},1
}
return false;
}
4 总结
解题的思想,主要是HashTable。想到就比较简单了。
5 参考
leetcode面试准备:Contains Duplicate I && II的更多相关文章
- LeetCode面试常见100题( TOP 100 Liked Questions)
LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...
- leetcode面试准备:Reverse Words in a String
leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- leetcode面试准备: Game of Life
leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- leetcode面试准备:Triangle
leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...
- leetcode面试准备:Sliding Window Maximum
leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...
随机推荐
- 一个java文件编译之后会产生多个class文件
如图所示:如果编译后一个java文件中类有内部类的话,就会编译产生多个类
- CSS样式表介绍
一. CSS中的样式选择 1)内样式(内联样式) style=””; 2)内嵌样式 <style type="text/css"></style> 3 ...
- HOOK函数(二)——全局HOOK
如果钩子函数定义于当前进程相关的线程中,则钩子函数只能处理当前进程的线程的消息,如果要想处理当前正在运行的所有进程的鼠标消息和键盘消息,那么安装钩子函数的代码必须实现在动态链接库中.所以如果想让安装的 ...
- iOS常见的设计模式
ios开发学习中,经常弄不清楚ios的开发模式,今天我们就来进行简单的总结和探讨~ (一)代理模式 应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则 ...
- 关于Spring AOP和IOC的一些总结
Spring官方网站:https://spring.io/ 最早对象的创建是有new关键字,但是如果产生的类比较繁多或者复杂,就用工厂代替new关键字,但是工厂的控制能力有限,譬如对产生对象的生命周期 ...
- Java学习笔记(基本数据类型和变量命名规则)
java基本数据类型 变量 1.变量就是可变的量. 2.常量就是不可变的量. 3.字面量:Java的变量和常量中存放的具体的数据成为字面量. 变量 命名规则: (1)首字母是英文字母.$或下划线,由字 ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- (转)Libevent(1)— 简介、编译、配置
转自:http://name5566.com/4190.html 参考文献列表:http://www.wangafu.net/~nickm/libevent-book/ 此文编写的时候,使用到的 Li ...
- bootstrap实现手风琴功能(树形列表)
首先把架包拷进项目,然后在页面中引进css,js <script src="js/jquery/jquery-2.1.1.min.js"></script> ...
- Linux文件权限学习总结
一.用户对文件或目录都有哪些权限? 四种:读.写.执行.没有权限 二.如何表示这四种权限? 如果用十进制数字表示,分别为:4.2.1.0:如果用字符表示,分别为:r.w.x.-.个人觉得,使用chmo ...