219. Contains Duplicate II【easy】
219. Contains Duplicate II【easy】
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
错误解法:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if (nums.empty()) {
return false;
}
unordered_map<int, int> my_map;
for (int i = ; i < nums.size(); ++i) {
if (my_map.find(nums[i]) == my_map.end()) {
my_map[nums[i]] = i;
}
else {
if (abs(i - my_map[nums[i]]) <= k) {
return true;
}
}
}
return false;
}
};

解法一:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if (nums.empty()) {
return false;
}
unordered_map<int, int> my_map;
for (int i = ; i < nums.size(); ++i) {
if (my_map.find(nums[i]) != my_map.end() && abs(i - my_map[nums[i]]) <= k) {
return true;
}
my_map[nums[i]] = i;
}
return false;
}
};
参考@jianchao.li.fighter 的代码
Well, the basic idea is fairly straightforward. We maintain a mapping mp from a value in nums to its position (index) i. Each time we meet an unseen value, we add it to the map (mp[nums[i]] = i). Otherwise, depending on whether the recorded index mp[nums[i]] and the current index i satisfy i - mp[nums[i]] <= k (node that the new index i is larger than the old index mp[nums[i]]), we return true or update the index (mp[nums[i]] = i). If all the elements have been visited and we have not returned true, we will return false.
解法二:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k)
{
unordered_set<int> s;
if (k <= ) return false;
if (k >= nums.size()) k = nums.size() - ;
for (int i = ; i < nums.size(); i++)
{
if (i > k) s.erase(nums[i - k - ]);
if (s.find(nums[i]) != s.end()) return true;
s.insert(nums[i]);
}
return false;
}
};
参考@luo_seu 的代码,就是维持固定那么多长度的数值
The basic idea is to maintain a set s which contain unique values from nums[i - k] to nums[i - 1],
if nums[i] is in set s then return true else update the set.
219. Contains Duplicate II【easy】的更多相关文章
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 680. Valid Palindrome II【easy】
680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...
- Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- 680. Valid Palindrome II【Easy】【双指针-可以删除一个字符,判断是否能构成回文字符串】
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 217. Contains Duplicate【easy】
217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
随机推荐
- 【AC自动机】zoj3228 Searching the String
对所有模式串建立AC自动机. 每个单词结点要记录该单词长度. 然后在跑匹配的时候,对每个单词结点再处理3个值,代表可重叠的匹配次数,不可重叠的匹配次数,以及“上一次不可重叠的匹配位置”,这样结合单词长 ...
- 【分块】bzoj3295 [Cqoi2011]动态逆序对
考虑每次删除pos位置一个数x后,所造成的的影响就是,逆序对的个数少了在1~pos-1中大于x的数的个数加上pos+1~n中小于x的数的个数. 那么我们需要的操作就只有查询区间内比某数大(小)的个数. ...
- 【最小路径覆盖】【二分图】【最大流】【Dinic】bzoj2150 部落战争
裸的最小路径覆盖. 把每个点拆点,变成二分图. 对于可以连边的点对(i,j):i->j'(1); 对于任意一点i,若i点为'.':S->i(1),i'->T(1); 答案为所有'.' ...
- python3开发进阶-Django框架中form的查看校验方法is_valid()的源码,自定义验证方法
form表单的校验方法is_valid() 点开我们发现这个函数里面只有两个方法方法,最终返回True or False 我们点进.is_bound属性,里面判断传输的数据不是空和上传文件不是空 点进 ...
- 1.4(学习笔记)JSP自定义标签
一.JSP自定义标签 JSP自定义标签,可以通过实现Tag接口.继承TagSupport类来设置标签功能. 后续通过配置文件将标签和具体的实现类关联. 二.自定义第一个标签(实现Tag接口) 自定义标 ...
- 数列求和 Exercise06_13
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:数列求和 * */ public class Exercise06_13 { public static void main( ...
- @requestBody注解的使用(下)
提示: 建议一定要看后面的@RequestBody的核心逻辑源码以及六个重要结论!本文前半部分的内容都是一些基本知识常 识,可选择性跳过. 说明: @RequestBody主要用来接 ...
- JSON.parse(str),JSON.stringify(a)
1.JSON.parse(str),字符串转换成对象 var str = '{"name":"huangxiaojian","age":&q ...
- BigDecimal的精度舍入模式详解
BigDecimal舍入模式介绍: 舍入模式在java.math.RoundingMode 里面: RoundingMode.CEILING :向正无限大方向舍入的舍入模式.如果结果为正,则舍入行为类 ...
- C#之Raw Socket网络封包监视源码
大家可以建立一个Windows Form应用程序,在下面的各个文件中添加对应的源码: //RawSocket.csnamespace ReceiveAll{ using System; using S ...