`

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.

题目中给定一个数组,判断数组中是否存在两个数nums[i]和nums[j],它们之间的差最大为t, 两个下标i和j之间的差最大为k。我们用TreeSet来解决,我们维护一个大小为k的treeset,然后通过subSet方法来判断在区间中是否存在和当前元素差小于等于t的元素。为了防止越界,我们将TreeSet设定成长整型。代码如下:
public class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if(nums == null || nums.length == 0 || t < 0) return false;
        TreeSet<Long> set = new TreeSet<Long>();
        set.add((long)nums[0]);
        for(int i = 1; i < nums.length; i++) {
            if(i > k) set.remove((long)nums[i - k - 1]);
            long left = (long)nums[i] - t;
            long right = (long)nums[i] + t;
            if(!set.subSet(left, right + 1).isEmpty()) return true;
            set.add((long)nums[i]);
        }
        return false;
    }
}
0
1
分享到:
评论

相关推荐

    cpp-算法精粹

    Contains Duplicate III Product of Array Except Self Game of Life Increasing Triplet Subsequence 单链表 Reverse Linked List Odd Even Linked List Add Two Numbers Reverse Linked List II Partition List ...

    kl.zip_KL熵_相对熵_相对熵 kl_相对熵KL_真实熵

    信息熵,是随机变量或整个系统的不确定性。熵越大,随机变量或系统的不确定性就越大。 相对熵,用来衡量两个取值为正的函数或概率分布之间的差异。 交叉熵,用来衡量在给定的真实分布下,使用非真实分布所指定的策略...

    leetcode下载-algorithm_practise:算法实践

    duplicate(滑动窗口), 454. 4sum 数组 leetcode: 基本排序算法 插入, 归并 -&gt; 递归 和 迭代 快排 -&gt; 随机 三路 leetcode75 数组遍历的为了减少时间复杂的的方法 对撞指针 leetcode: 167 two sum, 125回文串, 344 ...

    gasstationleetcode-leetcode_java:为求职做准备。Leetcode的java版本

    加油站 leetcode leetcode_java prepare for jobhunting. java version of Leetcode. easy 55 about takes ...4.Contains Duplicate II 5.Contains Duplicate 07/02/2015 1.Valid Parentheses 2.Maximum

    repository-beta:BETA-家庭助理社区附加组件

    BETA-家庭助理社区附加组件 警告! 这是一个测试版存储库 此Home Assistant加载项存储库包含加载项的beta版本。 他们可能会随时停止工作。 它们可能会对您的系统产生负面影响。 该存储库是为以下目的创建的: ...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    #217 Contains Duplicate #226 Invert Binary Tree #237 Delete Node in a Linked List #238 Product of Array Except Self #242 Valid Anagram #258 Add Digits #260 Single Number III #274 H-Index #283 Move ...

    javalruleetcode-leetcode-java:力码笔记

    java lru leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters ...III ...217.Contains Duplicate 263.Ugly Number 264.Ugly Number II

    leetcode浇花-LCSolutions:我的力扣解决方案

    leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer ...Contains Duplicate #0242 - Valid Anagram #0243 - Shortest Word Distance #0246 - Strobogrammatic Number #0263 -

    leetcode1004-leetcode:leetcode

    Contains Duplicate (E) 48. Rotate Image (M) -&gt; 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -&gt; 2 26. Remove Duplicates ...

    winutilities

    this suite contains utilities to clean registry, temporary files on your disks, erase your application and internet browser history, cache and cookies. you can control startup programs that load ...

    判断链表是否为回文链表leetcode-Algos_Practice:来自Leetcode、HackerRank等网站的练习题

    “contains_duplicate.py” - 检查整数列表是否包含任何重复项的快速算法。 “contains_nearby_duplicate.py” - 检查整数列表是否包含距离 k 内的任何重复项的快速算法。 "coin_arrangement.py" - 给定 n,算法找到...

    leetcode答案-code_challenges:代码挑战

    leetcode 答案Leet Code 挑战 这是我提交给 Lambda School CS Unit 2 构建周的已接受 ...Duplicates](https://leetcode.com/problems/contains-duplicate/) [x] [Linked List Cycle II](https://leetcode.co

    protel文件转换成pads文件

    This document contains information that is proprietary to Mentor Graphics Corporation. The original recipient of this document may duplicate this document in whole or in part for internal business ...

    addison.wesley.opengl.shading.language.2nd.edition.jan.2006

    In this chapter, shaders that duplicate some of the fixed functionality of the OpenGL pipeline are presented. Chapter 10 presents a few shaders that are based on the capability to store data in and ...

    lrucacheleetcode-leetcode-in-go:leetcode-in-go

    contains-duplicate-0217 find-minimum-in-rotated-sorted-array-0153 数组的乘积-除了-self-0238 从排序数组中删除重复项-0026 搜索旋转排序数组-0033 两个整数之和-0371 二和-0001 回溯 组合-和-0039 组合总和-ii-...

    tesseract4.0引擎,亲测可用.zip

    The scripts in this directory make it possible to duplicate the tests published in the Fourth Annual Test of OCR Accuracy. See http://www.isri.unlv.edu/downloads/AT-1995.pdf but first you have to ...

    Oracle sqldeveloper without jdk (win+linux)

    - Cannot export if result set contains duplicate column names. 2. Workarounds 2.1 To disable Code Insight Run SQL Developer from a command line using the following statement: Windows : sql...

    LeetCode最全代码

    260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](./C++/single-number-iii.cpp) [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium || 268| [Missing ...

    微软内部资料-SQL性能优化5

    However, if we are searching for multiple rows, such as duplicate values, or keys in a range, anything more than a small number of rows will make the nonclustered index search very inefficient. ...

    ConferenceTrackManagement.zip

    Each session contains multiple talks. Morning sessions begin at 9am and must finish before 12 noon, for lunch. Afternoon sessions begin at 1pm and must finish in time for the networking event. The ...

Global site tag (gtag.js) - Google Analytics