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 ...
随机推荐
- CSS实现背景透明,文字不透明(各浏览器兼容) (转)
/*CSS*/ .waps{ background:url(07158.bmp) no-repeat top center fixed; width:1004px; text-align:center ...
- js事件模型
连接在此 http://www.cnblogs.com/zqstc/archive/2009/11/26/1611464.html
- webrtc学习——mediaStream和MediaStreamTrack
This is an experimental technologyBecause this technology's specification has not stabilized, check ...
- Android应用Icon大小在不同分辨率下定义
http://www.ard9.com/gsjj/204.html 对于Android平台来说,不同分辨率下Icon的大小设计有着不同的要求,对于目前主流的 HDPI即WVGA级别来说,通常hdpi的 ...
- linq 多条件查询 where 拼接+分页
首先定义一个静态类 public static class QueryAssembly { /// <summary> /// 返回true /// </summary> // ...
- uiscrollview上的 uipangesturerecognizer冲突
最近在tableview里的cell imageview加了个 uipangesturerecognizer发现优先滚动imageview,往上拖的时候,tableView不响应滚动了,原来是tabl ...
- iOS NSNumber语法糖
BOOL equal; NSNumber * num1 = [NSNumber numberWithInteger:]; NSNumber * num2 = @; equal = [num1 isEq ...
- HDU 1176 免费馅饼(数字三角形)
免费馅饼 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉 ...
- IIS原理学习
IIS 原理学习 首先声明以下内容是我在网上搜索后整理的,在此只是进行记录,以备往后查阅只用. IIS 5.x介绍 IIS 5.x一个显著的特征就是Web Server和真正的ASP.NET Appl ...
- jQuery DataTables 插件使用笔记
初始化 在页面中 <!DOCTYPE html> <html> <head> <link rel="stylesheet" type=&q ...