leetcode笔记: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.

二. 题目分析

题目大意是,给定一个整数数组,判断其中是否存在两个不同的下标ij,满足:| nums[i] - nums[j] | <= t 且下标:| i - j | <= k

可以想到,维护一个大小为k的二叉搜索树BST,遍历数组中的元素,在BST上搜索有没有符合条件的数对,若存在则直接返回true,否则将当前元素数对插入BST,并更新这个BST(若此时BST的大小已为k,需要删掉一个值)。保证BST的大小小于或等于为k,是为了保证里面的数下标差值一定符合条件:| i - j | <= k。实现时,可使用mulitset来实现BST

mulitset是一个有序的容器,里面的元素都是排序好的,且允许出现重复的元素,支持插入,删除,查找等操作,就像一个集合一样。multiset内部以平衡二叉树实现。因此所有的操作的都是严格在O(logn)时间之内完成,效率较高。

三. 示例代码

// 方法一,由于vector无序,需要排列一次
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
         if(nums.size() < 2) return false;
         vector<pair<long, int>> value;
         for (int i = 0; i < nums.size(); ++i)
            value.push_back(pair<long, int>(nums[i], i));
         sort(value.begin(), value.end());
         for (int i = nums.size() - 1; i >= 1; --i)
         {
             for (int j = i - 1; j >= 0; --j)
             {
                 if (value[i].first - value[j].first > t) break;
                 else if (abs(value[i].second - value[j].second) <= k) return true;
                 else continue;
             }
         }
         return false;
    }
};
// 方法二
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
    multiset<int> window; // 维护一个大小为k的窗口
    for (int i = 0; i < nums.size(); ++i) {
        if (i > k) window.erase(nums[i - k - 1]); // 保持window内只有最新k个元素,间接保证窗口内各元素下标不超过k
        auto pos = window.lower_bound(nums[i] - t);
        if (pos != window.end() && *pos - nums[i] <= t) return true;
        window.insert(nums[i]);
    }
    return false;
}

四. 小结

练习了集合类set和mulitset的使用。相关题目有:Contains Duplicate II和Contains Duplicate I

时间: 2024-10-13 13:57:25

leetcode笔记:Contains Duplicate III的相关文章

[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

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

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解题笔记-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笔记: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. 二. 题目分析 题目的大意是,给定一个整数数组,推断数组中

Contains Duplicate III -leetcode

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

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a