【一天一道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 difference between i and j is at most k.

(二)解题

题目大意:给定一个数组,如果数组中存在连个相等的数之间的距离小于k,则返回true,反之返回false。

解题思路:只需要找到距离小于K的两个相等的数,很容易想到采用hash表的算法。

定义一个hashmap,hashmap[i]表示i这个数最近出现的位置。

当遍历到下一个i出现时,计算i-hashmap[i],如果小于k则代表存在,反之则不存在。

具体解释看代码:

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int,int>  hash;//STL中的hashmap为unordered_map
        int size = nums.size();
        for(int i = 0 ; i < size ; i++){
            if(hash.find(nums[i])!=hash.end()){//如果之前出现过nums[i]这个数,就计算距离
                if(i - hash[nums[i]]<=k) return true;
            }
            hash[nums[i]] = i;//保存最近出现nums[i]的位置
        }
        return false;
    }
};
时间: 2024-12-13 14:31:07

【一天一道LeetCode】#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

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

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

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

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