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,同时最大相差t的两个数。

1、第一次超时,用了很笨的办法,放入ArrayList中,但是由于特殊点,要用Long类型,然后每一次都需要排序,所以果断超时了。

public class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if (t < 0 || k < 1){
            return false;
        }
        if (k > nums.length - 1){
            k = nums.length - 1;
        }
        ArrayList<Long> list = new ArrayList();
        for (int i = 0; i <= k; i++){
            list.add((long)nums[i]);
        }
        Collections.sort(list);
        for (int i = 1; i < list.size(); i++){
            if (list.get(i) - list.get(i - 1) <= t){
                return true;
            }
        }
        for (int i = k + 1; i < nums.length; i++){
            list.remove((long)nums[i - k - 1]);
            list.add((long)nums[i]);
            Collections.sort(list);
            for (int j = 1; j < list.size(); j++){
                if (list.get(j) - list.get(j - 1) <= t){
                    return true;
                }
            }
        }
        return false;
    }
}

2、用map实现,类似于桶排序的原理,首先把数据都转成正数,并且使用long型(否则会出错,比如-2和2除以3都等于0,但是相距4大于3),同时桶(t) = t + 1(t == 0的情况要排出)

public class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if (k < 1 || t < 0){
            return false;
        }
        HashMap<Long, Long> map = new HashMap();
        for (int i = 0; i < nums.length; i++){

            long pos = (long) nums[i] - Integer.MIN_VALUE;
            long num = pos / ((long) t + 1);
            if (map.containsKey(num) || (map.containsKey(num - 1) && Math.abs(map.get(num - 1) - pos) <= t)
                                     || (map.containsKey(num + 1) && Math.abs(map.get(num + 1) - pos) <= t)){
                return true;
            }
            if (map.size() >= k){
                long delete = ((long) nums[i - k] - Integer.MIN_VALUE) / ((long) t + 1);
                map.remove(delete);
            }
            map.put(num, pos);
        }
        return false;
    }
}
时间: 2024-08-11 07:42:50

leetcode 220. Contains Duplicate III 求一个数组中有没有要求的元素 ---------- java的相关文章

求一个数组的最大k个数(java)

问题描述:求一个数组的最大k个数,如,{1,5,8,9,11,2,3}的最大三个数应该是,8,9,11 问题分析: 1.解法一:最直观的做法是将数组从大到小排序,然后选出其中最大的K个数,但是这样的解法,复杂度是O(logn*n),但是有时候并不需要排序,用简单的选择排序,或者是冒泡排序,那么就K轮的交换或者是选择,就可以得出结论,复杂度是O(n*k),当K很大的时候排序可能是更好的解法,当K小的时候用选择或者是冒泡效率会更加的高.但是这都是会对前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. 题意及分析: 给出一个数组,要求

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

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

(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, 每次检查新的值是否与原来窗口中的

java实现求一个数组里最大值和最小值之前缺省的数的算法

问题描述: 求一个数组里最大值和最小值之间缺省的数,例如 int arrDemo = {1, 3, 7};  那么就要输出最小值1和最大值7之间缺少的数字2,4,5,6 代码如下,有更好的思路欢迎大家在评论区留言讨论 1 package test; 2 3 public class Test { 4 5 static int[] array = { 6 -10,0,3,3,9 7 }; 8 9 private static void printEmptyItems(int[] array) {

《团队开发一(求一个数组的连续的子数组之和的最大值)》

(1)设计思想:一般的,求一个数组的最大子数组之和即是按数组顺序依次让前几个数的和与下一个数进行比较,设一变量来装每次比较后的较大的数,依此进行到数组终端:但是考虑到求的是连续的子数组,则应该想到除了在按顺序上的连续外,还得考虑到末端与首端的连续,所以按数组顺序依次求解得到的未必就是连续的最大的子数组之和,故此必须在此种情况下也求解出最大子数组之和,方法即是同时从数组的两端依次进行求出各自的最大子数组之和,然后在相遇前求和后与之前所求的最大子数组之和依次相比较,取它们中最大的一个作为连续的最大子

求一个数组的子数组的最大和

如题:求一个数组的子数组的最大和,要求O(n)时间复杂度. 由于有了O(n)时间复杂度的限制,所以暴力求解的O(n^2)方法肯定不行.再考虑递归求一个数组a[n]的子数组的最大和,可以分解为a[i]子数组的最大和以及a[n-i-1]之间的某种情况 a[n]的子数组最大和等于a[i]子数组的最大和: a[n]的子数组最大和等于a[n-i-1]: a[n]的子数组最大和跨a[i]和a[n-i-1]: 递归实现的时间复杂度为O(nlg(n)).最后考虑时间复杂度为O(n)的动态规划实现. /** *

算法 - 求一个数组的最长递减子序列(C++)

//**************************************************************************************************** // // 求一个数组的最长递减子序列 - C++ - by Chimomo // // 题目: 求一个数组的最长递减子序列,比方{8, 14, 6, 2, 8, 14, 3, 2, 7, 4, 7, 2, 8, 101, 23, 6, 1, 2, 1, 1}的最长递减子序列为{14.8,3.