[leetcode] Contains Duplicate II
2015-05-31 15:25
316 查看
From : https://leetcode.com/problems/contains-duplicate-ii/
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 i and j is
at most 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 i and j is
at most k.
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { map<int,int> mp; int len = nums.size(), num; for(int i=0; i<len; i++) { num = nums[i]; if(mp.find(num)==mp.end()) { mp[num] = i; } else { if(i-mp[num]<=k) return true; else { mp[num]=i; } } } return false; } };
相关文章推荐
- [leedcode219]Contains Duplicate II
- SGU119 Magic Pairs
- Internal Sorting: Straight Two-way Merge Sort: Sorting by Merging
- 535 Error:authentication failed
- Searching: Chained Hash Table Search And Insertion-1
- Searching: Chained Hash Table Search And Insertion-2
- uvalive 6886 Flowery Trails fft
- LeetCode Contains Duplicate
- LeetCode Contains Duplicate II
- #172 Factorial Trailing Zeroes
- AI初级模型
- Skill: Add new mail attention extension in Thunderbird
- leetcode Contains Duplicate II
- leetcode Contains Duplicate
- IDR_MAINFRAME 字符串 MFC
- #LeetCode OJ #11 Container With Most Water
- 关于Error: xz compression not available
- Solving "Vietnam snake" puzzle with constraint logic programming
- 使用行为树(Behavior Tree)实现游戏AI
- Failed to find provider info for com.google.settings