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 ...
随机推荐
- MySQL字段命名不能使用的MySQL关键字
#今天遇到一个问题,把某一字段重新命名为condition时报错,于是联想到可能是MySQL的关键字,用``引起来后,问题解决. #在MySQL数据库中,Table字段不能使用MySQL关键字: #[ ...
- 1.4 JAVA日期处理
一.JAVA日期 参考链接:https://www.runoob.com/java/java-date-time.html 1.日期两个构造函数 1.第一个构造函数使用当前日期和时间来初始化对象.Da ...
- CISCO实验记录一:路由器基本配置
一.路由器基本配置要求 1.设置路由器名为:hehe 2.设置特权模式下password为ccna,secret为ccnp,vty线路密码为ccie 3.所有明文密码都加密 二.路由器基本配置命令 1 ...
- QT中为程序加入超级管理员权限
QT的一些文件操作,注册表的操作等,有些操作会无效,主要是因为没有对C盘的相关权限. 解决方法: 1. mingw编译器 在pro工程文件中加入 RC_FILE=main.rc rc文件,之前一篇博客 ...
- mysql数据库——特殊sql语句整理之修改表结构
建表 先讲一下常规建表: CREATE TABLE testCreate ( id ) NOT NULL auto_increment, time ) NOT NULL, type ) NOT NUL ...
- linux下如何查看某个容器的详细信息?
答: 使用docker inspect <CONTAINER ID>即可
- python之NLP词性标注
1.知识点 包括中文和英文的词性标注主要使用的库是nltk和jiaba 2.代码 # coding = utf-8 import nltk from nltk.corpus import stopwo ...
- SPARQL查询语句整理
本文大多内容来自Joshua Taylor的回答 https://stackoverflow.com/users/1281433/joshua-taylor 查询子类或等价关系 https://sta ...
- CentOS 7系统配置上的变化
http://www.linuxidc.com/Linux/2014-09/107375p4.htm CentOS 7系统配置上的变化解析 ip ss指令替代 ifconfig route arp n ...
- 使用python装饰器计算函数运行时间的实例
使用python装饰器计算函数运行时间的实例 装饰器在python里面有很重要的作用, 如果能够熟练使用,将会大大的提高工作效率 今天就来见识一下 python 装饰器,到底是怎么工作的. 本文主要是 ...