219. Contains Duplicate II 超时

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int len=nums.size();
bool flag=false;
multimap<int ,int> mulm;
for(int i=0;i<len;i++)
{
mulm.insert(make_pair(nums[i],i));
}
multimap<int,int>::iterator it1=mulm.begin();
multimap<int ,int>::iterator it2=it1;
it2++;
for(;it2 !=mulm.end();it1++,it2++)
{
if((it2->first ==it1->first)&& (it2->second - it1->first<=k))
{
flag=true;
return true;
}

}
return false;
}

};

时间: 2024-07-30 14:38:28

219. Contains Duplicate II 超时的相关文章

leetCode 219. Contains Duplicate II 数组

219. Contains Duplicate II 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. 题目大意: 找到数组中两个相同元素,如果这两个元素的距离小于等于k

(easy)LeetCode 219.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 iand j is at most k. 方法1:暴力解法 代码如下: public class Solution { public boolean

LeetCode#219 Contains Duplicate II

Problem Definition: 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. Solution 1:  O(n^2)  (报超时) 1 def

219. Contains Duplicate II

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. 巧妙地利用字典 class Solution(object): def containsNearbyDuplicate

C#解leetcode 219. Contains Duplicate II

该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet 用法. 题目: 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 t

Java [Leetcode 219]Contains Duplicate II

题目描述: 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. 解题思路: HashMap解决. 代码如下: public class Solution { public

【一天一道LeetCode】#219. Contains Duplicate II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 differen

Java for LeetCode 219 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. 解题思路: HashMap,JAVA实现如下: public boolean containsNearby

LeetCode 219. Contains Duplicate II (包含重复项之二)

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 absolute difference between i and j is at most k. 题目标签:Array, Hash Table 题目给了我们一个nums array 和一个 k, 让