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.
关于上述题意,我们可以转化为两个数学公式:
存在|nums[i]-nums[j]|<=t且|i-j|<=k,为了计算方便,首先需要对前者进行变形。
|nums[i]-nums[j]|<=t -> |nums[i]/t-nums[j]/t|<=1 -> nums[j]/t - 1<= nums[i]/t <= nums[j]+1
则当nums[i]/t和nums[j]/t都同时取整,所以如果nums[i]/t能够满足上式,则取值只能为(nums[j]/t-1, nums[j], nums[j]+1)
from collections import OrderedDict class Solution:
def containsNearbyAlmostDuplicate(self, nums, k, t):
orderedDic = OrderedDict() if k < 1 or t < 0:
return False for each in nums:
key = each/max(1, t) for m in (key-1, key, key+1):
if m in orderedDic and abs(orderedDic[m] - each) <= t:
return True orderedDic[key] = each if len(orderedDic) > k:
orderedDic.popitem(last=False) return False
接下来,一行一行解释代码的意思。
第1行,from collections import OrderedDict。这里主要是需要使用OrderedDict.OrderedDict与一般的Dict最大的区别在于,它是有序的。
第8行,这里需要对没有意义的数据进行处理;
第12行,根据上面的推理,我们字典的键值定义为each/max(1,t),这里为什么是max(1,t),而不直接是max(t),这里主要是考虑t=0的情况;
第14-21行,对nums每个元素进行遍历,如果每一个元素m,将m/t作为键值,如果在orderedDic中,存在一个键值属于(m/t-1,m/t,m/t+1),则这就满足了上面推到的数学公式,但需要注意上面的数学公式并不是充要条件,所以需要判断abs(orderedDic[m] - each) <= t,当此时m/t不在orderedDic中,且orderedDic的长度<=k,则直接将此键值加入字典中,但是orderedDic的长度大于k,这时需要将最先加入的键值弹出,这也是使用orderedDic的原因。
可能有些同志存在疑问,为什么当字典长度大于k时,需要将多余的键值弹出,而且还要弹出第一个存入的键值?
|i-j|<=k这个条件就决定了,字典长度最大只能为k。假如,字典的长度为k+1,此时第k+2个元素与第一个元素满足|nums[0]-nums[k+1]|<=t条件,但它们不会满足|i-j|<=k, 因为|k+1-0|>=k。
Contains Duplicate III -leetcode的更多相关文章
- Contains Duplicate III —— LeetCode
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- Contains Duplicate,Contains Duplicate II,Contains Duplicate III
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- [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 ...
- [Leetcode] Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 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 suc ...
- LeetCode——Contains Duplicate III
Description: Given an array of integers, find out whether there are two distinct indices i and j in ...
随机推荐
- CDQ分治的嵌套
CDQ的嵌套 上一篇博客介绍了一下CDQ的入门思想.这里再介绍一下它的进阶,CDQ套CDQ.其实如果对入门思想掌握的透彻,嵌套也是很容易掌握的,思想是一样的. 什么是嵌套 简单地说,有的问题,如果用一 ...
- 微信sdk php签名方法整理
<?php class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appS ...
- Flutter制作Toast会自己关闭的消息提示框
项目中需要用到类似安卓的Toast提示框,因为flutter中又没有相关组件,然后在网上看到个不错的,地址https://www.jianshu.com/p/cf7877c9bdeb,然后拿过来修改了 ...
- 炉石兄弟 修复图腾师问题 by大神beebee102, 还有阴燃电鳗
修复图腾师问题 beebee102 修复了先摇图腾再下图腾师的问题,另外加了阴燃电鳗的sim卡.在兄弟策略的模拟程序中测试了没问题,真机没有试过,麻烦吧友测试了回复一下.链接: [有效] http ...
- 性能测试 | Linux系统top命令中的io使用率,很多人都误解了它的具体含义
body{ text-align:left; width:80%; margin:10px 100px; } 最近在做连续数据流的缓冲系统,C语言代码实现后,粗略测试了下,功能上应该没有问题.那么,接 ...
- nodejs服务端实现post请求
博客之前写过一篇php实现post请求的文章. 今天想到好久没有输出了,重新认识到输出的重要性.百般思索该写些什么?想来想去,想到了两点: 逐步熟练nodejs各种场景知识,针对mysql数据交互和f ...
- 【论文学习】Blind Super-resolution Image Reconstruction Based On Novel Blur Type Identification
机翻 <基于新型模糊类型识别的盲超分辨率图像重建>
- easyUI之Accordion(分类)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- RabbitMQ学习之:(二)介绍 (转贴+我的评论)
转自:http://lostechies.com/derekgreer/2012/03/05/rabbitmq-for-windows-introduction/ RabbitMQ for Windo ...
- 实验一 绘制任意斜率的直线段 | 使用VS2017工具
这世界上有很多坑,注定有些坑是要填的.下面我就用VS2017使用MFC对这个课堂实验进行填坑. 一.实验目的 (1)掌握任意斜率直线段的重点 Bresenham 扫描转换算法: (2)掌握 Cline ...