Leetcode: Contains Duplicate II
来源:互联网 发布:mac桌面图标消失 编辑:程序博客网 时间:2024/05/16 04:56
Question
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
Show Tags
Show Similar Problems
Solution
Analysis
A map is created to record whether an element appeared before as well as its ind.
class Solution(object): def containsNearbyDuplicate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: bool """ if nums==[] or len(nums)==1: return False dictin = {} for ind, elem in enumerate(nums): if elem in dictin: preind = dictin[elem] if abs(ind-preind)<=k: return True else: dictin[elem] = ind else: dictin[elem] = ind return False
0 0
- [LeetCode]Contains Duplicate II
- [LeetCode] Contains Duplicate II
- leetcode Contains Duplicate II
- LeetCode Contains Duplicate II
- [leetcode] Contains Duplicate II
- LeetCode Contains Duplicate II
- 【leetcode】Contains Duplicate II
- leetcode--Contains Duplicate II
- [leetcode]Contains Duplicate II
- [leetCode] Contains Duplicate II
- LeetCode: Contains Duplicate II
- LeetCode Contains Duplicate II
- LeetCode | Contains Duplicate II
- LeetCode Contains Duplicate II
- 【LeetCode】Contains Duplicate II
- leetcode: Contains Duplicate II
- LeetCode Contains Duplicate II
- Leetcode: Contains Duplicate II
- Hello Jni
- iOS第三管理工具pods
- 社説 20150829 自民党総裁選 無投票再選も前向きな選択肢
- Android手游发行兵器谱
- 对面向过程,对象,接口,切面的理解
- Leetcode: Contains Duplicate II
- zoj 1453 Surround the Trees Andrew算法
- 前端的色彩选择
- DirectX3D游戏开发五 简单模型创建(索引缓存)
- 阿里校招内推总结
- iOS开发者,你不可错过的资源
- 机器学习算法与Python实践之(二)支持向量机(SVM)初级
- void * 指针介绍
- ACM学习-匈牙利匹配