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.

ref: http://bookshadow.com/weblog/2015/06/03/leetcode-contains-duplicate-iii/

好牛逼 “滑动窗口”  这里的做法

,新学了一个结构 treeset

下面引用书影的解释“

解法II:“滑动窗口” + TreeSet

参考LeetCode Discuss:https://leetcode.com/discuss/38177/java-o-n-lg-k-solution

TreeSet数据结构(Java)使用红黑树实现,是平衡二叉树的一种。

该数据结构支持如下操作:

1. floor()方法返set中≤给定元素的最大元素;如果不存在这样的元素,则返回 null。

2. ceiling()方法返回set中≥给定元素的最小元素;如果不存在这样的元素,则返回 null。

有个容易八阿哥的地方

n<=t+set.floor(n))

不能写成n-t《=。。。

public class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {

        if(k<1||t<0) return false;
        TreeSet<Integer> set = new TreeSet<Integer>();
        for(int i=0;i<nums.length;i++){
            int n = nums[i];
            if((set.floor(n)!=null&& n<=t+set.floor(n))||(set.ceiling(n)!=null && set.ceiling(n)<=t+n))
                return true;
            set.add(n);
            if(i>=k)
                set.remove(nums[i-k]);
        }
        return false;

    }
}
时间: 2024-11-08 18:51:29

Contains Duplicate III的相关文章

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

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

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 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 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: 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来解决,sortedSet可

LeetCode——Contains Duplicate III

Description: 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.判断是否满

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