Leetcode: Contains Duplicate II
2015-08-29 10:26
369 查看
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.[code]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
相关文章推荐
- 服务器维护手记-crontab引起大量sendmail进程打死内存问题
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2)
- Gmail打不开登录不了邮箱最新解决方法!
- leetcode 219: Contains Duplicate II
- Gym 100338H High Speed Trains(高精度)
- Reflow & Repaint
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 1) C. CNF 2 无向图找环
- wait、sleep、notify、notifyAll的区别
- 不使用Main.storyboard
- 11 Container With Most Water
- XCode Could not launch "" failed to get the task for process
- leetcode: (219) Contains Duplicate II
- Wait Condition 例子
- 生产者与消费者 & wait()& notify()
- _itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }2
- _itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }
- getpagesize.c:32: __getpagesize: Assertion `_rtld_global_ro._dl_pagesize != 0' failed
- android.intent.action.MAIN与android.intent.category
- vim 配置替换错误E488: Trailing characters
- unity3d 游戏人工智能开发之状态机(C#模板与示例)