[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.



数组中是否存在两个元素,他们的值小于等于t,并且下标小于等于k。

Javascript时间很宽松,暴力O(n^2)直接可以过,但这题应该是希望我们用二叉搜索树。

可以把k看做滑动窗口,对于一个新的数,可以想象成放到这个窗口的最左边或者最右边,如果这个数+t或-t之后落在窗口里,直接返回true。

https://leetcode.com/discuss/38177/java-o-n-lg-k-solution

Java BST:

 1 public class Solution {
 2     public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
 3         TreeSet<Integer> treeSet = new TreeSet<Integer>();
 4         for(int i = 0; i < nums.length; i++){
 5             Integer floor = treeSet.floor(nums[i] + t);
 6             Integer ceiling = treeSet.ceiling(nums[i] - t);
 7             if( (floor != null && floor >= nums[i])
 8                 || (ceiling != null && ceiling <= nums[i]) ) return true;
 9             treeSet.add(nums[i]);
10             if(treeSet.size() > k) treeSet.remove(nums[i - k]);
11         }
12         return false;
13     }
14 }

Brute Force:

 1 /**
 2  * @param {number[]} nums
 3  * @param {number} k
 4  * @param {number} t
 5  * @return {boolean}
 6  */
 7 var containsNearbyAlmostDuplicate = function(nums, k, t) {
 8     for(var i = 0; i < nums.length; i++)
 9         for(var j = i + 1; j < nums.length; j++)
10             if(Math.abs(nums[i] - nums[j]) <= t && Math.abs(i - j) <= k)
11                 return true;
12     return false;
13 };
时间: 2024-08-27 01:01:53

[LeetCode][Java]Contains Duplicate III的相关文章

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

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. 题意及分析: 给出一个数组,要求

(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 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

Best Time to Buy and Sell Stock III leetcode java

题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time

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

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:Contains Duplicate - 判断数组内是否有重复元素

1.题目名称 Contains Duplicate(判断数组内是否有重复元素) 2.题目地址 https://leetcode.com/problems/contains-duplicate/ 3.题目内容 英文: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