【leetcode题解】【99.38】【E】Contains Duplicate II
2015-12-17 19:28
204 查看
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.
Subscribe to see which companies asked this question
维持一个k大小的滑动窗口,需要考虑的就是边界问题,略麻烦。
判断窗口k里面是否有重复元素,可以利用集合,看看“集合化”之后,大小是否发生变化。
竟然跑出了击败99.38的速度,可见并不是很笨。
between i and j is at most k.
Subscribe to see which companies asked this question
维持一个k大小的滑动窗口,需要考虑的就是边界问题,略麻烦。
判断窗口k里面是否有重复元素,可以利用集合,看看“集合化”之后,大小是否发生变化。
竟然跑出了击败99.38的速度,可见并不是很笨。
class Solution(object): def containsNearbyDuplicate(self, nums, k): if not nums: return 0 < k if len(nums) == 1 or k == 0: return False if len(nums) <= k: s = set(nums) return len(s) < len(nums) i = 0 while i < len(nums)-k: t = nums[i:i+k+1] i += 1 s = set(t) if len(s) < len(t): return True return False
相关文章推荐
- 使用mailx发送邮件
- 开源项目学习笔记-Kuaihu(仿知乎日报)
- 什么是 Conversion to Dalvik format failed ?
- 从Container内存监控限制到CPU使用率限制方案
- UVa 11448 - Who said crisis?
- 再叙TIME_WAIT
- Codeforces Round #335 (Div. 1) A. Sorting Railway Cars
- 56: Recv failure: Connection was reset和55错误解决办法
- 使用UstraISO制作ISO文件教程
- DG ServiceClient Failover
- How To Configure Client Failover For Dataguard Connections Using Database Services (Doc ID 1429223.1
- .net winform的IsMdiContainer属性
- AIX下彻底删除websphere安装文件
- VS2013 Git 无法 push:An error was raised by libgit2. Category = Net (Error)
- Why Copied Yocto Cross Toolchain Not Work
- main函数的参数
- Could not launch “XXXX” process launch failed: Security
- HibernateException: Could not obtain transaction-synchronized Session for current thread
- LeetCode Container With Most Water
- db2 优化基础 打开EXPLAIN