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 ...
随机推荐
- jqery筛选
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- zzzzw_在线考试系统①准备篇
在弄完购物系统之后,小博也了解了解怎么用struts这个框架捣鼓一个在线考试系统 购物系统用的是MVC模式,现在这个struts2原理上也是基于MVC模式的.那么要做这个东西之前先了解一下难点在哪里 ...
- 关于U3D画面出现卡顿的问题
在U3D中,曾近遇到过卡顿的问题,下面说明解决方法 一:在关于相机移动的函数中,移动的函数不应该放在Update里面应该放到LateUpdate 二:如果最开始建立项目的时候选择的时候是3D游戏,如果 ...
- Js中的appenChild,insertBefore--createDocumentFragment
平时项目中会有一些流程,或者是评论相关的东西,这些一般只会是在页面初次加载一部分,剩余部分搞一个更多的标签,当点击更多的时候,ajax请求把所有数据加载完(当然这里也有分页的实现方法,本篇不作讨论), ...
- /etc/sysconfig/目录详解
/etc/sysconfig/目录包括了在红帽企业LINUX下各种系统配置文件,以下是在/etc/sysconfig/目录下的文件列表如图: 500)this.width=500;"> ...
- linux yum配置
yum源模版 vi /etc/yum.repos.d/xxx.repo [rhel-server]name=serverbaseurl=file:///media/disk/Serverenabled ...
- 九度OJ 1107 搬水果 -- 哈夫曼树 2011年吉林大学计算机研究生机试真题
题目地址:http://ac.jobdu.com/problem.php?pid=1107 题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果 ...
- IOS 学习笔记 2015-04-15 手势密码(原)
// // WPSignPasswordView.h // 网投网 // // Created by wangtouwang on 15/4/9. // Copyright (c) 2015年 wan ...
- ubuntu 下安装软件,卸载,查看已经安装的软件
参考网址:http://wiki.ubuntu.org.cn/UbuntuSkills 一般的安装程序用三种: .deb 和.rpm 这两种安装文件 .bundle 这是二进制的安装文件 而 tar. ...
- php url字符转义操作
遇到一段代码,从数据库里读出来带 \ 字符 需要转义成中文~ 用到url_decode(); //$info 为刚从数据库中读取的二维数组 foreach($info as $key1 => & ...