[抄题]:

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.

Example 1:

Input: nums = [1,2,3,1], k = 3, t = 0
Output: true

Example 2:

Input: nums = [1,0,1,1], k = 1, t = 2
Output: true

Example 3:

Input: nums = [1,5,9,1,5,9], k = 2, t = 3
Output: false

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

bucket值可能很小,所以用 让分子变大。

 - Integer.MIN_VALUE

[思维问题]:

以为2个指针,没想到果然有技巧

[英文数据结构或算法,为什么不用别的数据结构或算法]:

水桶排序的精髓在于容量恒定,重复出现的放在下一个水桶。此题要求数字差值恒定,所以可以用水桶排序。

做法:/容量即可。类似取余数。

[一句话思路]:

差值不超过t, 而且越近越好。在k范围内的bucket值重复出现肯定可以,相邻的检查一下也可以。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. map里存的是对象,变量类型都是大写开头。只能用put方法。其他一般的变量类型用小写开头即可。
  2. long remappedNum = (long) nums[i] - Integer.MIN_VALUE;每个变量都要改成long
  3. 求map整个一坨的方法是:
if (map.keySet().size() > k)

[二刷]:

long型放在分母时,要加括号

/ ((long)t + 1);

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

nums[i - k] 要不要-1 自己试试就行了,也是debug的一个重点

[总结]:

水桶排序要求不能重复。做法:/容量即可。类似取余数。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
//cc
if (nums == null || nums.length == 0) return false;
if (k < 1 || t < 0) return false; //ini: HashMap, bucket
HashMap<Long, Long> map = new HashMap<>(); //for loop: judge as true in 3 cases, add(bucket, value) to map while maintain k
for (int i = 0; i < nums.length; i++) {
long appendedNum = (long)nums[i] - Integer.MIN_VALUE;
long bucket = appendedNum / ((long)t + 1); //judge as true in 3 cases
if (map.containsKey(bucket) ||
(map.containsKey(bucket - 1) && Math.abs(appendedNum - map.get(bucket - 1)) <= t) ||
(map.containsKey(bucket + 1) && Math.abs(map.get(bucket + 1) - appendedNum) <= t)) return true; //while maintain k numbers in the map
if (map.entrySet().size() >= k) {
long lastBucket = ((long)nums[i - k] - Integer.MIN_VALUE) / ((long)t + 1);
map.remove(lastBucket);
} //put to map
map.put(bucket, appendedNum);
} return false;
}
}

220. Contains Duplicate III 数组指针差k数值差t的更多相关文章

  1. [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)

    二分.情况讨论 因为数组有序,所以能够考虑用二分.通过二分剔除掉肯定不是第k位数的区间.如果数组A和B当前处理的下标各自是mid1和mid2.则 1.假设A[mid1]<B[mid2], ①.若 ...

  2. 【medium】220. Contains Duplicate III

    因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...

  3. 220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. [Leetcode] 220. Contains Duplicate III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  5. 220 Contains Duplicate III 存在重复 III

    给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...

  6. LeetCode 220. Contains Duplicate III (分桶法)

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  7. [LeetCode] 220. Contains Duplicate III 包含重复元素 III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  8. [刷题] 220 Contains Duplicate III

    要求 给出整型数组nums和整数k,是否存在索引i和j 使得nums[i]和nums[j]之差不超过t,且i和j之差不超过k 思路 建立k个元素的有序查找表 每次有新元素加入,寻找查找表中大于 num ...

  9. (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 suc ...

随机推荐

  1. PythonStudy——第一阶段性测试

    1.Python解释器,在2.x和3.x上分别采用的是什么默认编码8 2.定义字符串变量时,单引号,双引号,三引号什么区别? 3.编程语言可以分为哪三类,特点都是什么 4.定义一个变量有三个特性, 5 ...

  2. day02-python与变量

    1.堆区开辟空间存放 变量值  2.将存放 变量值 空间的地址提供给栈区  3.栈区为变量名开辟空间存放提供来的地址 变量直接相互赋值 定义变量的优化机制 定义变量与重新赋值

  3. JAVA面向对象设计中类关系

    现在看以前描述有点小问题:类之间关系分为继承.泛化.依赖.关联.聚合.聚合关系几种.继承是is a关系,泛化(类实现接口)表示like a关系. 类之间的关系种类: Generalization(泛化 ...

  4. 代码问题:【CF2】

    [CF2/CFCF/HCF]: C Ma, JB Huang, X Yang, et al. Hierarchical convolutional features for visual tracki ...

  5. ubuntu下的Nessus插件更新

    00x1: 记录下nessus插件离线更新,免得每次度娘我Nessus是放在虚拟机里面. 00x2: nessus 插件更新地址: https://plugins.nessus.org/v2/offl ...

  6. .NET自动化测试工具链:Selenium+NUnit+ExtentReport

    Selenium可以执行UI的交互,ExtentReport用来生成测试报告,NUnit是我熟悉的基础测试框架,当然你也可以用MSTest.Xunit来代替.Selenium.NUnit没啥好讲的,网 ...

  7. JavaWeb——XML转义符字

    被<![CDATA[]]>这个标记所包含的内容将表示为纯文本,比如<![CDATA[<]]>表示文本内容“<”.  此标记用于xml文档中,我们先来看看使用转义符的 ...

  8. syslog-ng日志收集分析服务搭建及配置

    syslog-ng日志收集分析服务搭建及配置:1.网上下载eventlog_0.2.12.tar.gz.libol-0.3.18.tar.gz.syslog-ng_3.3.5.tar.gz三个软件: ...

  9. python3之os、sys

    os模块 # 显示当前使用平台:"nt":windows;"posix":Linux >>> os.name 'nt' # 当前工作目录 &g ...

  10. oninput、onchange与onpropertychange事件的区别, 与input输入框实时检测

    这几天项目着急,同时也学到好多以前没有接触过的知识.oninput.onchange与onpropertychange事件的区别, 与input输入框实时检测 onchange事件只在键盘或者鼠标操作 ...