给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的 绝对值 至多为 k。
示例 1:
输入: nums = [1,2,3,1], k = 3 输出: true 示例 2:
输入: nums = [1,0,1,1], k = 1 输出: true 示例 3:
输入: nums = [1,2,3,1,2,3], k = 2 输出: false
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/contains-duplicate-ii 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution(object): def containsNearbyDuplicate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: bool """ dict={} for index,i in enumerate(nums): if i not in dict: dict[i]=index else: if index-dict[i]<=k: return True else: dict[i]=index return False执行结果:
通过
显示详情
执行用时:28 ms, 在所有 Python 提交中击败了65.61%的用户
内存消耗:16.7 MB, 在所有 Python 提交中击败了100.00%的用户
