- 浏览: 182786 次
- 性别:
- 来自: 济南
文章分类
最新评论
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设定成长整型。代码如下:
题目中给定一个数组,判断数组中是否存在两个数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; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 264Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 266You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 381Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 372Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 560Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 474Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 661Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 468The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 427Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 571Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 576Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 423All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 894Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 928Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 669Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 841Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 780You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 703For a undirected graph with tre ...
相关推荐
python python_leetcode题解之220_Contains_Duplicate_III.py
python python_leetcode题解之217_Contains_Duplicate.py
java入门 java_leetcode题解之219_Contains_Duplicate_II
python python_leetcode题解之219_Contains_Duplicate_II.py
信息熵,是随机变量或整个系统的不确定性。熵越大,随机变量或系统的不确定性就越大。 相对熵,用来衡量两个取值为正的函数或概率分布之间的差异。 交叉熵,用来衡量在给定的真实分布下,使用非真实分布所指定的策略...
duplicate(滑动窗口), 454. 4sum 数组 leetcode: 基本排序算法 插入, 归并 -> 递归 和 迭代 快排 -> 随机 三路 leetcode75 数组遍历的为了减少时间复杂的的方法 对撞指针 leetcode: 167 two sum, 125回文串, 344 ...
加油站 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
BETA-家庭助理社区附加组件 警告! 这是一个测试版存储库 此Home Assistant加载项存储库包含加载项的beta版本。 他们可能会随时停止工作。 它们可能会对您的系统产生负面影响。 该存储库是为以下目的创建的: ...
#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 ...
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 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer ...Contains Duplicate #0242 - Valid Anagram #0243 - Shortest Word Distance #0246 - Strobogrammatic Number #0263 -
Contains Duplicate (E) 48. Rotate Image (M) -> 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) -> 2 26. Remove Duplicates ...
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 ...
0603rl - duplicate 0805-100V 0805CL 0805cl - duplicate 0805RL 1206 1206CL 1206RL 1210 1812 2512 3225[1210] 6332[2512] BR_RS25XX C822j823聚丙烯 CAP3 CAP11-8 CAP22.5*10 CD1 CD1B CD1S CD2 CD2.5-6.3 CY1 ...
“contains_duplicate.py” - 检查整数列表是否包含任何重复项的快速算法。 “contains_nearby_duplicate.py” - 检查整数列表是否包含距离 k 内的任何重复项的快速算法。 "coin_arrangement.py" - 给定 n,算法找到...
《手稿_V1.111》探讨了一个在C++编程环境下解决LeetCode中的数据结构问题,具体题目是“Contains Duplicate II”。这个问题涉及到数组、滑动窗口和哈希映射等概念,要求在给定整数数组`nums`和一个整数`k`的情况下,...
题目217——存在重复元素(Contains Duplicate): 这道题目要求我们检查给定的整数数组`nums`中是否存在重复的元素。如果存在重复元素,函数应返回`true`,否则返回`false`。 **解决方案一:哈希表(Hash Table)*...
- **Contains Duplicate**:检查数组中是否存在重复的元素。哈希表可以用于跟踪已遇到的元素。 - **Jewels and Stones**:计算在给定的字符集合(jewels)中,有多少字符出现在另一个字符集合(stones)中。哈希表...
leetcode 答案Leet Code 挑战 这是我提交给 Lambda School CS Unit 2 构建周的已接受 ...Duplicates](https://leetcode.com/problems/contains-duplicate/) [x] [Linked List Cycle II](https://leetcode.co