LeetCode Contains Duplicate II (判断重复元素)
2015-07-03 23:12
771 查看
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { if(nums.empty()) return false; unordered_map<int,int> mapp; nums.insert(nums.begin(),0); for(int i=1; i<nums.size(); i++){ if(!mapp[nums[i]]) mapp[nums[i]]=i; //第一次出现 else{ if(i-mapp[nums[i]]<=k ) return true; //已经出现,且符合条件 mapp[nums[i]]=i; //不符合,更新最近一个位置 } } return false; } };
AC代码
相关文章推荐
- ubuntu用户添加adduser, useradd (http://blog.csdn.net/lele52141/article/details/6593840)
- HDU1789Doing Homework again(贪婪)
- tail
- 对话Facebook人工智能实验室主任、深度学习专家Yann LeCun
- mybaits使用存储过程
- LeetCode-Factorial Trailing Zeroes-解题报告
- Copy Failed Error Access to fobidden
- 关于main函数中argc和argv的简单介绍
- Diesel engine has become the agent involving detail
- 我们“真的 ”知道自己在说什么吗?
- warning C4819: The file contains a character that cannot be represented in the current code page (93
- Running the Cloudera Training VM in VirtualBox(转)
- AIDL学习
- HMailServer日记
- 多线程performSelectorOnMainThread
- 小作:brainfuck解释器
- git push error: RPC failed; result=22, HTTP code = 411
- launch failed.Binary not found.的解决方案之一
- 最严谨的校验email地址的正则表达式及各种语言对应版
- Linux系统调用--------wait() 与 waitpid()