LeetCode219: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.

直接使用循环时间复杂度为O(N*k),使用stl中的基于红黑树实现的map能降低循环的时间,在O(logN)的时间复杂度内找到元素。或者更进一步使用基于hash表的unordered_map。在常数时间内找到元素。这道题学习到的两个经验:

  1. 当想在常数时间内完毕查询工作时考虑hash表。

  2. stl中实用hash表实现的数据结构map和set,所以它们也保留了hash表查询上的特点。

代码例如以下:

class Solution {
public:

/*  解法一:超时
    bool containsNearbyDuplicate(vector<int>& nums, int k) {

        int length=nums.size();
        if(length<=1||k<=0) return false;

        //将每个元素与其后面k个元素进行比較
        for(int i=0;i<length;i++)
        {
            for(int j=1;j<=k&&(i+j)<length;j++)
            {
                if(nums[i]==nums[i+j])
                    return true;
            }
        }
        return false;
    }
    */

   //解法二,利用stl中的map。记录下整数以及它的下标
    bool containsNearbyDuplicate(vector<int>& nums, int k)
    {
        //之前直接使用的是map。时间是96ms,后来把map替换成unordered_map时间变成了32ms
        unordered_map<int ,int> maps;
        int length=nums.size();
        for(int i=0;i<length;i++)
        {
            if(maps.count(nums[i]))//假设在maps中找到了该元素。推断它们位置是否小于等于k
            {
                if((i-maps[nums[i]])<=k)
                    return true;
            }
            maps[nums[i]]=i;
        }
        return false;
    }

};

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-10-13 00:40:42

LeetCode219:Contains Duplicate II的相关文章

LeetCode之“散列表”:Contains Duplicate &amp;&amp; Contains Duplicate II

 1. Contains Duplicate 题目链接 题目要求: 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 the array, and it should return false if every element is distinct. 代码如下:

【LeetCode-面试算法经典-Java实现】【219-Contains Duplicate II(包含重复元素II)】

[219-Contains Duplicate II(包含重复元素II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 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] = n

Leetcode解题笔记-Contains Duplicate &amp;&amp; Contains Duplicate II&amp;&amp;Contain Duplicate III

Contain Duplicate 题目要求: 给定一个数组如果有重复就返回true,如果没有就返回false 个人分析: 这个题比剔除重复的要简单许多,只需要判断就好 代码如下: public static boolean containsDuplicate(int[] nums){ if (nums.length==0) return false; Arrays.sort(nums); for(int i =1; i < nums.length; i++){ if(nums[i-1]==nu

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] Contains Duplicate(II,III)

Contains Duplicate 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 the array, and it should return false if every element is distinct. 解题思路 用一个set保存数组中的值,如

Contains Duplicate,Contains Duplicate II,Contains Duplicate III

217. Contains Duplicate 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 the array, and it should return false if every element is distinct. 判断数组是是否含有重复元素.

leetcode_219题——Contains Duplicate II(哈希表)

Contains Duplicate II Total Accepted: 13284 Total Submissions: 51898My Submissions Question Solution 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] a

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 II

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. 算法思想 思想一: 两个数组下标i,j,在叫j-i<