Leetcode 220 Contains Duplicate III

1. 问题描述

  给定一个整数数组nums[],查找是否存在两个下标i和j,满足|numsi?numsj|≤t 且 |i?j|≤k。


2. 方法与思路

  总得思路就是:“滑动窗口”+unordered_map。

  推理过程如下:

  |numsi?numsj|≤t?|numsi/t?numsj/t|≤1;

  由上式可以推出:|?numsi/t???numsj/t?|≤1

  等价于:?numsi/t?∈{?numsj/t?,?numsj/t??1,?numsj/t?+1}。

  我们只需要维持一个大小为K的窗口,键值为?numsi/t?, 值为numsi。然后循环遍历nums[]数组,与unordered_ map中键集对应的值进行差得绝对值运算,再判断即可。

  对于STL中关于unordered_map的相关可参考官方References 对unordered_map讲解

注意: unordered_map是一种特殊的hash map,它不按照键值进行排序。通过hash表的算法,查找效率为O(1)。但会消耗一定内存。进行差值运算时需转换为long类型,否则会溢出。

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        if(t < 0 | k <1)
            return false;

        int i,key;
        unordered_map<int,int> dict;

        for(i =0; i < nums.size(); i++)
        {
            key = nums[i]/max(1,t);
            //map<int,int>::iterator it;
            if( (dict.find(key) != dict.end() && abs(nums[i] - dict[key]) <= t) ||
                (dict.find(key-1) != dict.end() && abs((long)nums[i] - (long)dict[key-1]) <= t) ||
                (dict.find(key+1) !=dict.end() && abs(nums[i] - dict[key+1]) <= t))
            {
                return true;
            }

            //dict[key] = nums[i];
            dict.insert(pair<int,int>(key,nums[i]));

            if(i >= k)
            {
                dict.erase(nums[i-k]/max(1,t)); //删除窗口大小之外的键值
            }

        }
        return false;
    }
};
时间: 2024-12-28 21:03:32

Leetcode 220 Contains Duplicate III的相关文章

leetcode 220. Contains Duplicate III 求一个数组中有没有要求的元素 ---------- java

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. 找出数组中有没有最多相距k,同时最大相差

[LeetCode] 220. Contains Duplicate III Java

题目: Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. 题意及分析: 给出一个数组,要求

Java for LeetCode 220 Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 解题思路: 暴力枚举会LTE,解题思路是用sortedSet来解决,sort

(medium)LeetCode 220.Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 思想借鉴:维持一个长度为k的window, 每次检查新的值是否与原来窗口中的

[LeetCode][Java]Contains Duplicate III

Contains Duplicate III Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 数组中是否存在两个元素,他们的

【LeetCode】220. Contains Duplicate III

题目: Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 提示: 看到题目之后,马上想到了题目应该是要利用一个长度为k的划窗,

220. Contains Duplicate III

1 class Solution { 2 public: 3 bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { 4 if (nums.size() < 2) return false; 5 multimap<int, int> m; 6 for (int i = 0; i < nums.size(); ++i) { 7 m.insert(pair<int, int&g

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解题笔记-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