LeetCode 220. Contains Duplicate III

简介: 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。

v2-361532d1ca618ba08048c890b3f2df5f_1440w.jpg

Description



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


描述



给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。


示例 1:

输入: nums = [1,2,3,1], k = 3, t = 0

输出: true


示例 2:

输入: nums = [1,0,1,1], k = 1, t = 2

输出: true


示例 3:

输入: nums = [1,5,9,1,5,9], k = 2, t = 3

输出: false


思路



  • 这道题使用有序字典这种数据结构,使用桶排序的思想.
  • 我们把把数字放到不同的桶内,桶的范围为i*t~(i+1)*t,于是差值为t的数一定出现在当前数字应该防止的桶内,当前桶的上一个桶,当前桶的下一个桶。我们把这三个桶的值和当前值做差,如果差值小于等于t说明存在满足题意的解,如果不存在则返回False.
  • 由于所以相差为k,所以当字典中存储的数超过k个的时候,我们就可以把先插入的数字弹出.


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-01-25 19:57:09
# @Last Modified by:   何睿
# @Last Modified time: 2019-01-25 21:09:12
import collections
class Solution:
    def containsNearbyAlmostDuplicate(self, nums, k, t):
        """
        :type nums: List[int]
        :type k: int
        :type t: int
        :rtype: bool
        """
        if not nums or k <= 0 or t < 0: return False
        # 创建一个有序字典
        dic = collections.OrderedDict()
        for n in nums:
            # 这里使用了桶的思想
            key = n if not t else n // t
            for m in (dic.get(key - 1), dic.get(key), dic.get(key + 1)):
                if m is not None and abs(n - m) <= t:
                    return True
            # False表示先进先出
            if len(dic) == k: dic.popitem(False)
            dic[key] = n
        return False


源代码文件在这里.


目录
相关文章
|
4月前
Duplicate keys detected: '0'原因及解决方法
Duplicate keys detected: '0'原因及解决方法
LeetCode 316. Remove Duplicate Letters
给定一个仅包含小写字母的字符串,去除字符串中重复的字母,使得每个字母只出现一次。需保证返回结果的字典序最小(要求不能打乱其他字符的相对位置)。
61 0
LeetCode 316. Remove Duplicate Letters
|
索引
LeetCode 287. Find the Duplicate Number
给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
84 0
LeetCode 287. Find the Duplicate Number
|
算法 Python
LeetCode 217. 存在重复元素 Contains Duplicate
LeetCode 217. 存在重复元素 Contains Duplicate
LeetCode 219: 存在重复元素 II Contains Duplicate II
题目: ​ 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k。 ​ Given an array of integers and an integer k, find o...
605 0
LeetCode 217:存在重复元素 Contains Duplicate
题目: 给定一个整数数组,判断是否存在重复元素。 Given an array of integers, find if the array contains any duplicates. 如果任何值在数组中出现至少两次,函数返回 true。
9575 0