Contains Duplicate II leetcode
2016-01-09 16:28
344 查看
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 jis at most k.
Subscribe to see which companies asked this question
Subscribe to see which companies asked this question
bool containsNearbyDuplicate(vector<int>& nums, int k) { unordered_map<int, int> hash; int i = 0; for (auto num : nums) { unordered_map<int, int>::iterator iter = hash.find(num); if (iter == hash.end()) hash.insert(make_pair(num, i)); else { if (i - (*iter).second <= k) return true; (*iter).second = i; } ++i; } return false; }
相关文章推荐
- 【转载】Kafka High Availability
- Contains Duplicate leetcode
- Codeforces Round #180 (Div. 2) B. Sail 贪心
- 职责链模式(Chain of Responsibility)
- offsetof与container_of宏[总结]
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog
- Codef b125 orces 615B Longtail Hedgehog 【dp】
- UVa 474 - Heads / Tails Probability
- mailx定时任务不能执行
- B. Longtail Hedgehog【记忆化搜索】
- [CodeForces 615B]Longtail Hedgehog[DP]
- Binder与Service 通信机制详解四 (源码分析AIDL工作机制)
- Codeforces Round #338 (Div. 2)B. Longtail Hedgehog(贪心+dp)
- UltraISO软碟通怎么装系统 UltraISO软碟通不用U盘装系统图文教程
- 从一个简单的main方法执行谈谈JVM工作机制
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog dp
- Codeforces 615B Longtail Hedgehog 【dp】
- Codeforces Round #338 (Div. 2)B. Longtail Hedgehog解题报告
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog 记忆化搜索/树DP
- 进程通信:管道和socketpair的