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 ...
随机推荐
- NHibernate中的IQueryable和IQueryover
今天在做一个小项目时,用到了NHibernate,使用了模糊查询(Like),在后台用IQueryable去接收Session.Query<T>()的查询结果. 代码如下: /// < ...
- Android在onCreate()中获得控件尺寸
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt ...
- 在office 2010中插入Mathtype出现ctrl+v不能复制的问题
加载项中去掉command for那个即可 详细网址:http://heblue.blog.163.com/blog/static/96325568201375102628405/
- C#学习笔记(3)
先理解一下方法重写和方法重载这2个概念: 1.方法重写(override):发生在父子类之间,子类重写父类中的方法,关键字是override. 2.方法重载(overload):一个类中有多个重名的方 ...
- ASP.NET页面生命周期总结(1)
图解:1) 浏览器 :把用户的操作封装成一个请求通过socket发送到后台服务器. 后台服务器:首先有个内核模块Http.sys 和针对每个应用程序池都有一个请求队列.然后请求到达http.sys ...
- java 知识 链接
www.java2blog.com/2016/04/hashmap-in-java-with-examples.html HashMap全剖析 http://card.weibo.com/articl ...
- C++ 变量转换
atoi,atol,strtod,strtol,strtoul实现类型转换2006-02-13 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://ivanvic.blogb ...
- UItextField常用方法
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view ...
- webapi之jsonp调用
定义跨域handle public class CorsHandler : DelegatingHandler { const string Origin = "Origin"; ...
- Unity中使用RequireComponent,没有添加上组件
using UnityEngine; using System.Collections; [RequireComponent(typeof(MeshFilter), typeof(MeshRender ...