leetcode之219. Contains Duplicate II(C++解法)
2016-09-11 20:10
423 查看
题目:
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 j is at most k.
[b]***************************[/b]我是分割线[b]*******************[/b]
class Solution {
public:
bool containsNearbyDuplicate(vector& nums, int k) {
};
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 j is at most k.
[b]***************************[/b]我是分割线[b]*******************[/b]
class Solution {
public:
bool containsNearbyDuplicate(vector& nums, int k) {
unordered_map <int,int> coun; for(int i=0; i<nums.size(); i++) { if (coun[nums[i]]) { if( i + 1 - coun[nums[i]] <= k) return true; else coun[nums[i]] = i + 1; } else coun[nums[i]] = i + 1; } return false; }
};
相关文章推荐
- leetcode_c++:Contains Duplicate II(219)
- leetcode 219 Contains Duplicate II C++
- Leetcod-219_Contains Duplicate II-滚动滑块解法-【C++】
- Leetcode 219 Contains Duplicate II
- LeetCode#219 Contains Duplicate II
- [Leetcode 219, Easy] Contains Duplicate II
- Java for LeetCode 219 Contains Duplicate II
- LeetCode219:Contains Duplicate II
- leetcode 219:Contains Duplicate II
- Contains Duplicate II - LeetCode 219
- 【LeetCode-面试算法经典-Java实现】【219-Contains Duplicate II(包含重复元素II)】
- LeetCode219:Contains Duplicate II
- Leetcode[219]-Contains Duplicate II
- LeetCode 219:Contains Duplicate II
- [Leetcode]#219 Contains Duplicate II
- 【LeetCode】219. Contains Duplicate II
- LeetCode219——Contains Duplicate II
- leetcode_219题——Contains Duplicate II(哈希表)
- leetcode 219: Contains Duplicate II
- leetcode: (219) Contains Duplicate II