[leetcode][hash] Contains Duplicate II
2015-07-08 15:46
561 查看
题目:
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.
维护一个长度为k的窗口
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.
维护一个长度为k的窗口
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { if (nums.size() < 2 || k <= 0) return false; int start = 0, end = 0; set<int> hashTable; hashTable.insert(nums[0]); for (int i = 1; i < nums.size(); ++i){ set<int>::iterator iter = hashTable.find(nums[i]); if (iter != hashTable.end()) return true; hashTable.insert(nums[i]); end = i; if (end - start == k){ hashTable.erase(nums[start]); ++start; } } return false; } };
相关文章推荐
- Dynamics AX 2012 R2 安装 AIF IIS上的Web服务
- _ _type_traits
- Hadoop错误集:Could not find the main class: org.apache.hadoop.*
- copy与retain copy和mutableCopy区别
- hive subprocess failed with code X 的错误码对应信息
- email之TO、CC、BCC意义
- kindle3折腾之修砖、修复“wifi unavailable”等总结
- NDK 编译armebai-v7a的非4字节对齐crash Fatal signal 7 (SIGSEGV) 错误解决
- BZOJ 3172 [Tjoi2013]单词 AC自动机(fail树)
- svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
- Command /usr/bin/codesign failed with exit code 1
- <BaiduMapAPI/BMapKit.h>file not found
- 控制台下创建窗口和WinMain入口创建窗口简单程序
- svn: E230001: Server SSL certificate verification failed
- Directional Shadow Details 平行光阴影细节
- 011 Complaint
- 【1】MainActivity监听器使用
- Context-Dependent Pre-Trained Deep Neural Networks for Large-Vocabulary Speech Recognition
- Contains Duplicate
- Contains Duplicate II