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

题意及分析:

给出一个数组,要求判断是否存在,对于其中两个不同的元素有:(1) |i-j| <= k (2) |nums[i]-nums[j]| <= t。

方法一:TreeSet(binary search tree)  o(Nlogk)

利用treeset,用floor方法查找treeSet中大于等于当前值且与当前元素距离小于t的最大元素,然后如果该元素不为空,那么就可以找到符合题意的两个元素。

或者用用ceiling方法查找treeSet中大于等于当前值且与当前元素距离小于t的最小元素,然后如果该元素不为空,那么就可以找到符合题意的两个元素。

最后需要注意的一点,就是两个元素之间的距离要小于等于k,即|i-j|<=k,所以当前treeSet中最多只能有k个元素。

代码:

class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
       if(nums==null||nums.length==0||k<=0) return false;

       TreeSet<Long> treeSet = new TreeSet<Long>();
       for(int i=0;i<nums.length;i++){

           final Long floor = treeSet.floor((long)nums[i]+t);
           final Long ceil = treeSet.ceiling((long)nums[i]-t);

           if((floor!=null&&floor>=(long)nums[i])||
                   (ceil!=null&&ceil<=(long)nums[i])){
               return true;
           }

           treeSet.add((long)nums[i]);
           if(i>=k){        //因为元素的坐标差不能超过k,所以在treeSet中最多只能有k个元素
               treeSet.remove((long)nums[i-k]);
           }
       }
        return false;
    }
}

方法二:窗口滑动  o(n)

  

时间: 2024-10-12 11:43:29

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

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,同时最大相差

(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

【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的划窗,

Leetcode 260. Single Number III JAVA语言

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 

220. Contains Duplicate III

1 class Solution { 2 public: 3 bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { 4 if (nums.size() < 2) return false; 5 multimap<int, int> m; 6 for (int i = 0; i < nums.size(); ++i) { 7 m.insert(pair<int, int&g

Contains Duplicate,Contains Duplicate II,Contains Duplicate III

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. 判断数组是是否含有重复元素.

[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. 数组中是否存在两个元素,他们的