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 ...
随机推荐
- 在windows下安装lxml 报错error: Unable to find vcvarsall.bat
刚开始安装 ,我是使用命令pip install lxml直接进行安装,不过出错了 error: Unable to find vcvarsall.bat 解决方案: 1.首先安装wheel,pip ...
- 图解golang内存分配机制 (转)
一般程序的内存分配 在讲Golang的内存分配之前,让我们先来看看一般程序的内存分布情况: 以上是程序内存的逻辑分类情况. 我们再来看看一般程序的内存的真实(真实逻辑)图: Go的内存分配核心思想 G ...
- 2018-2019-2 20175226王鹏雲 实验四《Android程序设计》实验报告
2018-2019-2 20175226王鹏雲 实验四<Android程序设计>实验报告 实验报告封面 课程:Java程序设计 班级:1752班 姓名:王鹏雲 学号:20175226 指导 ...
- tp5 模型关联,多表联查实用方法
1.模型中建立关联关系 public function goods(){ return $this->belongsTo('app\common\model\goods\Goods', 'goo ...
- 黑马vue---21-22、总结
黑马vue---21-22.总结 一.总结 一句话总结: · 在 VM 实例中,如果要访问 data 上的数据,或者要访问 methods 中的方法, 必须带 this · 在 v-for 要会使用 ...
- POCO C++库笔记 【1.Foundation基础库的结构】
Foundation库是POCO的基础库,提供了一些C++编程中常用的功能的抽象封装,主要由以下这些部分组成: Core -- 这部分除了建立跨平台库的基础头文件外,最有意义的部分是分装了原子计数的 ...
- hibernate一对一映射
package loaderman.c_one2one; // 身份证 public class IdCard { // 身份证号(主键) private String cardNum;// 对象唯一 ...
- Activity切换动画
下一页动画 trans_in.xml <?xml version="1.0" encoding="utf-8"?> <translate an ...
- grivaty,margin和padding的区别
layout_margn是指组件距离父窗体的距离, padding是指组件中的内容距离组件边缘的距离 Layout_grivaty与grivaty的区别 layout_grivaty是指组件相对父窗体 ...
- UM概述
历史 UML创始于1994年10月,主要创始人Grady Booch.Jim Rumbaugh和Ivar Jacobson. UML(Unified modeling language统一建模语言) ...