您的位置:首页 > 编程语言 > Python开发

LeetCode OJ 系列之219 Contains Duplicate II --Python

2015-11-23 14:49 615 查看
Problem:

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 jis
at most k.

Answer:

class Solution(object):
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if k==0 : return False
if k>= len(nums) and len(set(nums)) < len(nums): return True
for i in range(len(nums)-k):
if len(nums[i:i+k+1])>len(set(nums[i:i+k+1])): return True
return False
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode Python OJ