[leetcode] Contains Duplicate II
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.
class Solution
{
public:
bool containsNearbyDuplicate(vector<int> &nums, int k)
{
set<int> S;
int start = , end = ; for(int i=; i<nums.size(); i++)
{
if(S.find(nums[i]) != S.end())
return true;
else
{
S.insert(nums[i]);
end++;
} if(end-start > k)
{
S.erase(nums[start]);
start++;
}
} return false;
}
};
class Solution
{
public:
bool containsNearbyDuplicate(vector<int> &nums, int k)
{
map<int, int> M; for(int i=; i<nums.size(); i++)
{
if(M.find(nums[i]) != M.end() && i-M[nums[i]] <= k)
return true;
else
M[nums[i]] = i;
} return false;
}
};
[leetcode] Contains Duplicate II的更多相关文章
- [LeetCode] Contains Duplicate(II,III)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
- LeetCode Contains Duplicate II (判断重复元素)
题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false. 思路:用map记录每个出现过的最近的位置,扫一边序列即可.扫到一个元素就判断它在前面什么地方出现过.本题数据有点 ...
- leetcode Contains Duplicate II python
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- LeetCode & Q219-Contains Duplicate II
Array Hash Table Description: Given an array of integers and an integer k, find out whether there ar ...
- LeetCode——Contains Duplicate II
Description: Given an array of integers and an integer k, find out whether there there are two disti ...
- leetcode:Contains Duplicate和Contains Duplicate II
一.Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your fun ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
随机推荐
- andriod手机签到应用服务器设计
最近导师要求我和另一个同学开发一个手机上课签到应用,我负责客户端和服务器之间的通信架构编写和数据的存储 本人大学四年只用过汇编和C/C++,因此对andriod开发还是一窍不通,花了一个星期写出来了基 ...
- [Design Patterns] 2. Design principle
Single Responsibility Principle 类的设计趋向于:Use Case Diagram --> (derived) --> Detail Open-Closed ...
- Android下拉刷新底部操作栏的隐藏问题
最近自己编写下拉刷新的时候,发现了一个问题,就是有一个需求是这样的:要求页面中是一个Tab切换界面,一个界面有底部操作栏,不可下拉刷新,另一个界面没有底部操作栏,但可以下拉刷新. 按照平常的做法,我在 ...
- Const的用法
宏和const的区别: 1.宏执行的是替换操作,这也就意味着会在内存中开辟多个临时空间 这样显然不是很好 2.宏不可以修改 const : 用const修饰的变量 为常量 不能修改,在内存中只有一份内 ...
- MySQL忘记root用户密码修改方法
一般来说在MySQL修改用户密码有好几种方法: 1.修改自己的密码可用: set password=password('123456'); 2.修改其它用户的密码可用: set password fo ...
- [Architect] Abp 框架原理解析(4) Validation
本节目录 介绍 DataAnnotations ICustomValidate IShouldNormalize 实现Abp Validation 介绍 Abp中在Application层集成了val ...
- 重构第31天 使用多态替代条件语句( Replace conditional with Polymorphism)
理解:本文中的”使用多态代替条件判断”是指如果你需要检查对象的类型或者根据类型执行一些操作时,一种很好的办法就是将算法封装到类中,并利用多态性进行抽象调用. 详解:本文展示了面向对象编程的基础之一“多 ...
- for循环的嵌套,for循环的穷举迭代
for循环的嵌套 输入一个正整数,求阶乘的和 嵌套 Console.Write("请输入一个正整数:"); int ...
- How to manage the certificates in the PC
1.open Run command. 2.enter 'mmc' . 3.Click File, and Add or Remove Snap-in. 4.Select Certificates, ...
- C#更改win7系统时间的代码,以及为什么更改不成功
我在用C#更改win7系统的时间时(必须用管理员的权限,点击要运行程序,鼠标右键“以管理员权限运行”),下面列出了3张图片,使用第一张的代码执行不成功,使用第二张图片可以执行成功,第三张图片是说明原因 ...