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 ...
随机推荐
- 洛谷 P1852 [国家集训队] 跳跳棋
题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...
- 英尺和米之间的转换 Exercise06_09
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:英尺和米之间的转换 * */ public class Exercise06_09 { public static void ...
- Java高级架构师(一)第08节:基本业务功能和数据字典
- Klaus Aschenbrenner--windbg
http://www.sqlservercentral.com/blogs/aschenbrenner/?page=1
- 集群Cluster介绍
来源:http://www.ibm.com/developerworks/cn/linux/cluster/lw-clustering.html简单的说,集群(cluster)就是一组计算机,它们作为 ...
- mormot 直接使用UNIDAC引擎操作数据库
mormot 直接使用UNIDAC引擎操作数据库 MORMOT封装了BDE.FIREDAC.UNIDAC.Nexus 四种通用型数据库引擎,形成了自己独特的数据引擎控件.前提条件是首先要安装通用型数据 ...
- sqlmapapi的跨域访问Access-Control-Allow-Origin:*;ajax
1.做sqlmapapi的二次开发时,需要通过ajax方式调用sqlmapapi,但是默认情况下,sqlmapapi是不允许跨域访问的 2.尝试增加ajax的header,修改origin的值,来避免 ...
- kernel简介
内存管理 一般来看有三种类型的地址:物理地址.线性地址和逻辑地址,逻辑地址的精髓在于将地址分成两部分:段基地址+偏移,翻译的过程如下: 线性地址的精髓在于将所有的内存按照一定的大小分成了一页一页,对多 ...
- (原创)sklearn中 F1-micro 与 F1-macro区别和计算原理
最近在使用sklearn做分类时候,用到metrics中的评价函数,其中有一个非常重要的评价函数是F1值,(关于这个值的原理自行google或者百度) 在sklearn中的计算F1的函数为 f1_sc ...
- PHPCMS V9管理员password忘记怎样改动
一般的虚拟主机商都提供了PHPmyAdmin,选择你站点数据库.然后选择v9_admin这个表. 编辑 password,变成:fa3250300be9b7ab0848257f3cbb06e7 enc ...