Contains Duplicate III —— LeetCode

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,下标i和j相差<=k。

解题思路:题目没说数组有序,那就得按照无序处理,可以排序,然后从头遍历;或者用个BST之类的有序数据结构,遍历数组的时候重建一下,重建的过程中判断是否有合法的解,有就返回true,否则重建完之后返回false。

public class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if(nums==null||nums.length==0||k<=0){
            return false;
        }
        TreeSet<Integer> tree = new TreeSet<>();
        for(int i=0;i<nums.length;i++){
            Integer big = tree.floor(nums[i]+t);//floor返回集合里<=指定元素的最大值
            Integer small = tree.ceiling(nums[i]-t);//celing返回集合里>=指定元素的最小值
            if((big!=null&&big>=nums[i])||(small!=null&&small<=nums[i])){
                return true;
            }
            if(i>=k){
                tree.remove(nums[i-k]);
            }
            tree.add(nums[i]);
        }
        return false;
    }
}
时间: 2024-10-29 06:03:57

Contains Duplicate III —— LeetCode的相关文章

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

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 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. 参考资料: https://leetcode.com/discuss/381

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可