leetcode:219 Contains Duplicate II-每日编程第三十五题
2015-12-26 19:52
393 查看
Contains Duplicate II
Total
Accepted: 38552 Total
Submissions: 137098 Difficulty: 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 difference between i and j is
at most k.
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int size=nums.size();
map<int,int> ma;
map<int,int>::iterator it;
for(int i=0;i<size;i++){
if((it=ma.find(nums[i]))!=ma.end()){
if((i-(it->second))<=k){
return true;
}else{
it->second=i;
}
}else{
ma[nums[i]]=i;
}
}
return false;
}
};
Total
Accepted: 38552 Total
Submissions: 137098 Difficulty: 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 difference between i and j is
at most k.
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int size=nums.size();
map<int,int> ma;
map<int,int>::iterator it;
for(int i=0;i<size;i++){
if((it=ma.find(nums[i]))!=ma.end()){
if((i-(it->second))<=k){
return true;
}else{
it->second=i;
}
}else{
ma[nums[i]]=i;
}
}
return false;
}
};
相关文章推荐
- 好的代码行
- python pip源
- 字符串操作(C++程序设计第7周)
- 【JAVA笔记——道】并发编程CAS算法
- GPU 编程入门到精通(五)之 GPU 程序优化进阶
- leetcode:58 Length of Last Word-每日编程第三十四题
- 如何创建和使用Python CGI脚本
- 整数的输出格式(C++程序设计第7周)
- 实数的输出格式(C++程序设计第7周)
- 服务相关编程
- java取出一组中文每个汉字的首字母 如 :中国 zg
- 对编程而言,学历再高,智商再高,也得一样一样来
- Python模块学习:threading 多线程控制和处理
- ACM StepByStep(一)基础知识与参考资料
- Java之关于Serialization
- 位操作在java和C/C++中的区别
- Django项目开发纪要
- 深刻理解Python中的元类(metaclass)
- GitHub简明使用指南
- 他山之石-PHP多线程的实现方法详解