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】的更多相关文章

  1. 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 ...

  2. 680. Valid Palindrome II【easy】

    680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 217. Contains Duplicate【easy】

    217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...

  8. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  9. 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 ...

随机推荐

  1. 【set】【multiset】bzoj1058 [ZJOI2007]报表统计

    对n个位置,每个位置维护一个vector. 每次插入,可能对MIN_SORT_GAP产生的影响,只可能是 插入元素 和 它的 前驱 后继 造成的,用一个set维护(存储所有序列中的元素). 我们还得维 ...

  2. 洛谷 P2066 机器分配

     题目背景 Background 无  题目描述 Description 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能 ...

  3. jvm-监控指令-jdump

    格式: jmap [option] vmid 作用: 生成堆转储快照. 使用:(注意:需要使用工具打开,分析. 比如: EclipseMemoryAnalyzer)

  4. Idea下Android的配置

    (1) 下载安装好Intellij Idea和Android SDK. (2) Android SDK设置 ,在FIle –> Other Settings –> Default Proj ...

  5. jquery给input标签添加data-options属性

    //原生JS实现document.getElementById('startPrice').setAttribute("data-options", "required: ...

  6. 十个Chatbot框架介绍

    十个Chatbot框架介绍 原创 2016年12月13日 16:01:23 4616 Chatbot列表 1.  Artificial Intelligence Markup Language    ...

  7. Spring的学习(IoC,AOP)等

    下面这个系列是非常好的例子: http://www.yiibai.com/spring/spring-3-hello-world-example.html 正在看,把一些基础夯实. IoC可以从下面一 ...

  8. scrapy-splash抓取动态数据例子十四

    一.介绍 本例子用scrapy-splash爬取超级TV网站的资讯信息,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4. ...

  9. 查看MySQL数据库大小

    查看MySQL数据库大小 1.首先进入information_schema 数据库(存放了其他的数据库的信息) ? 1 2 mysql> use information_schema; Data ...

  10. eclipse 配置maven 项目tomcat 运行