LeetCode219——Contains Duplicate II
2015-07-31 18:22
288 查看
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 iand j is
at most k.
实现:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> maps;
for (int i = 0; i < nums.size(); i++) {
auto search = maps.find(nums[i]);
if(search != maps.end()) {
if (i - search->second <=k){
return true;
} else {
search->second = i;
}
} else {
maps.insert(std::make_pair(nums[i], i));
}
}
return false;
}
};
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 iand j is
at most k.
实现:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> maps;
for (int i = 0; i < nums.size(); i++) {
auto search = maps.find(nums[i]);
if(search != maps.end()) {
if (i - search->second <=k){
return true;
} else {
search->second = i;
}
} else {
maps.insert(std::make_pair(nums[i], i));
}
}
return false;
}
};
相关文章推荐
- hdu杭电1789 Doing Homework again【优先队列+贪心】
- HDOJ 1789 Doing Homework again(贪心,好题)
- PicklingError: Can't pickle <type 'thread.lock'>: attribute lookup thread.lock failed
- 阿里云SSH "Write failed: Broken pipe"的问题解决方法
- Doing Homework again
- HDOJ Doing Homework again (贪心+优先队列)
- 2015 Multi-University Training Contest 4--多校赛第四场--做完了再添加上来
- hdu 5335 Walk Out bfs 2015 Multi-University Training Contest 4 09
- http://blog.csdn.net/xingfuzhijianxia/article/details/6433918
- Brainman
- POJ_2709_Painter
- GdiplusFlat(2)WM_PAINT消息,窗口子类化和设备上下文
- Revealing that beginning will let you include foreseeable future high priced diesel-engined sign maintenance tasks
- ubuntu 安装rails 遇到的问题及解决办法
- Hdu 2015 Multi-University Training Contest4
- Error creating bean with name 'empresasDAO': Injection of autowired dependencies failed up vote 0
- 2015多校联合训练第三场Painter(hdu5319)
- DLL and PLL for detail statement
- epoll机制:epoll_create、epoll_ctl、epoll_wait、close
- Error: failed to find a main class in