[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 ...
随机推荐
- JS中style属性
解决办法:1.先定义一个CSS规则,然后this.className=''2.document.getElementByIdx_x("a").style.cssText=" ...
- HIVE: SerDe应用实例
数据文件内容 id=123,name=steven id=55,name=ray 期望输出格式 123 steven 55 ray 1. 创建表, 用正则表达式的形式指定格式 create table ...
- Powerdesigner逆向工程从sql server数据库生成pdm (转载)
第一步:打开"控制面板"中的"管理工具" 第二步:点击"管理工具"然后双击"数据源(odbc)" 第三步:打开之后,点击 ...
- C#获取类以及类下的方法(用于Asp.Net MVC)
在C#中,实现动态获取类和方法主要通过反射来实现,要引用System.Reflection. public ActionResult GetControllerAndAction() List< ...
- storm分组模式
Shuffle grouping: Tuples被随机分配到每一个bolt’s task,以便于每一个bolt’s task获得相同数量的tuples. Fields grouping: Stream ...
- 2014 网选 5014 Number Sequence(异或)
/* 题意:a, b两个序列,规定由[0, n]区间的数! 求 a[i] ^ b[i] 的和最大! 思路:如果数字 n的二进制有x位, 那么一定存在一个数字m,使得n^m的所有二进制位 都是1,也就是 ...
- LeetCode——Find the Duplicate Number
Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...
- zabbix配fpmmm(mpm)数据传送不了问题解决
我们环境用zabbix mpm来监控mysql,不过最近官网已经不叫mpm了,而是叫fpmmm,理由为: fpmmm is the successor of mpm. mpm was renamed ...
- Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划)
Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到 ...
- 11.22 点餐APP第一阶段总结
第一个冲刺结束了,任务也算是完成了. 团队合作不像单独做那样想怎么来就怎么来,各个人都不同的意见,最后方案的需要每个人一致同意通过才能执行. 不过团队合作分配到每个人的任务也相对轻一点,而且遇到问题解 ...