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的更多相关文章

  1. Contains Duplicate III —— LeetCode

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

  2. Contains Duplicate III

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

  3. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  4. [LeetCode] Contains Duplicate III 包含重复值之三

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

  5. LeetCode(220) Contains Duplicate III

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

  6. [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 ...

  7. [Leetcode] Contains Duplicate III

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

  8. 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 ...

  9. LeetCode——Contains Duplicate III

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

随机推荐

  1. Python爬虫 Urllib库的基本使用

    1.构造Requset 其实上面的urlopen参数可以传入一个request请求,它其实就是一个Request类的实例,构造时需要传入Url,Data等等的内容.比如上面的两行代码,我们可以这么改写 ...

  2. MIME协议(一) -- RFC822邮件格式

    MIME协议(一) -- RFC822邮件格式 .   如同其他各种电子文档一样,电子邮件内容也必须遵循一定的格式要求,各种邮件处理程序才能从中分析和提取出发件人.收件人.主题和附件等信息.邮件内容的 ...

  3. SSH工具--FinalShell

    FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发,运维工具,充分满足开发,运维需求.特色功能:免费海外服务器远程桌面加速,ssh加速,双边tcp加速,内网穿 ...

  4. javascript操作表单

    表单元素除了可以运用上述所有DOM相关操作外,为了简化,还有一系列自己的属性和方法. 表单除了支持鼠标,键盘,更改和html时间之外,还支持一些表单特有的事件,如focus,change,blur等等 ...

  5. iOS开发-多层嵌套block中如何使用__weak和__strong

    1.关于__weak__weak只能在ARC模式下使用,也只能修饰对象(比如NSString等),不能修饰基本数据类型(比如int等)__weak修饰的对象在block中不可以被重新赋值.__weak ...

  6. 手把手教你设置MongoDB密码

    mongodb密码和传统数据如mysql等有些区别: mongodb的用户名和密码是基于特定数据库的,而不是基于整个系统的.所有所有数据库db都需要设置密码. 1. 查看所有数据库(在mongodb新 ...

  7. docker打包项目部署

    1.首先在本地安装docker,步骤可参考https://www.cnblogs.com/conswin/p/11055853.html 2.在对应项目目录下增加Dockerfile文件,其内容可参考 ...

  8. jsp页面,使用Struts2标签,传递和获取Action类里的参数,注意事项。<s:a action><s:iterator><s:param>ognl表达式

    在编写SSH2项目的时候,除了使用<s:form>表单标签向Action类跳转并传递参数之外,很更多时候还需要用到<s:a action="XXX.action" ...

  9. Q窗口操作函数(窗口最大化,全屏,隐藏最大化最小化按钮)

    //Qt主窗口没有最小化,最大化按钮且最大化显示  int main(int argc, char *argv[]) { QApplication a(argc, argv); TestQtForWi ...

  10. [go]etcd使用

    // 连接etcd import ( "github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/mvcc/mvc ...