【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的划窗,那么问题就在于用什么数据结构来构造这个划窗。由于当新的数字进来后,我们需要比较它和划窗内其他数字的大小关系,从搜索的效率角度出发,可以使用BST,因此我们选择STL中的SET容器来构造这个划窗。

思路也很简单,当新进来一个数后,先在容器中寻找是否有比nums[i]-t大的数字,这一步可以利用lower_bound函数实现,之后再比较比nums[i]-t大的数字中最小的那个与nums[i]的差,如果符合条件就返回true。

另外不要忘记划窗移动时,删除和插入相应的元素。

代码:

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        if (k < 0) {
            return false;
        }
        set<int> window;
        for (int i = 0; i < nums.size(); ++i) {
            if (i > k) {
                window.erase(nums[i-k-1]);
            }
            auto pos = window.lower_bound(nums[i] - t);
            if (pos != window.end() && *pos - nums[i] <= t) {
                return true;
            }
            window.insert(nums[i]);
        }
        return false;
    }
};
时间: 2024-10-09 19:57:08

【LeetCode】220. Contains Duplicate III的相关文章

【LeetCode】-- 260. Single Number III

问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, b). 要求常量空间,线性时间. 问题解决: 这题用到“神奇的位运算”. 1.因为除了特殊的两个元素,其余两两出现,那么就想到了XOR(异或). 2.从头到尾XOR之后,会得到a xor b 的结果.接下来我们试着把这两个元素分离开来. 3.我们在a xor b 中找到任意一位XOR[diff_po

【LeetCode】216. Combination Sum III

Combination Sum III Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascend

【LeetCode】217. Contains Duplicate (2 solutions)

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】437. 路径总和 III

437. 路径总和 III 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / 5 -3 / \ 3 2 11 / \ 3 -2 1 返回 3.和等于 8

【leetcode】1278. Palindrome Partitioning III

题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindro

【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 class Solution { 2 public: 3 bool containsNearbyDupl

【LeetCode】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. 这题利用unordered_map来解决应该还是很简单的. class

【leetcode】260. Single Number III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The order

【LeetCode】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. Solution 1: sort后相邻位比较 1 class Solution