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,则返回true。如果任意两个相同元素的距离都大于k,或者没有两个元素是相同的,那么返回false。

代码如下:

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int ,int> myMap;
        for(int i = 0;i < nums.size();i++)
        {
            if(myMap.find(nums[i]) == myMap.end())
            {
                myMap.insert(pair<int,int>(nums[i],i));
            }
            else
            {
                if(i - myMap[nums[i]] <= k)
                {
                    return true;
                }
                else
                    myMap[nums[i]] = i;
            }
        }
        return false;
    }
};

2016-08-12 01:54:29

时间: 2024-07-29 01:10:51

leetCode 219. Contains Duplicate II 数组的相关文章

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, 让

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

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

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

(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 STL

1 class Solution { 2 public: 3 bool containsNearbyDuplicate(vector<int>& nums, int k) { 4 map<int,int> mii; 5 for (int i = 0;i<nums.size() ;++i) 6 { 7 if (mii.find(nums[i]) != mii.end()) 8 { 9 if(i - mii[nums[i]] <= k) return true; 1

LeetCode:Contains Duplicate II - 判断数组内是否有重复元素2

1.题目名称 Contains Duplicate II(判断数组内是否有重复元素2) 2.题目地址 https://leetcode.com/problems/contains-duplicate-ii/ 3.题目内容 英文: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] = nu

LeetCode:Contains Duplicate - 判断数组内是否有重复元素

1.题目名称 Contains Duplicate(判断数组内是否有重复元素) 2.题目地址 https://leetcode.com/problems/contains-duplicate/ 3.题目内容 英文:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in