#-*- coding: UTF-8 -*-
#遍历所有元素,将元素值当做键、元素下标当做值
#存放在一个字典中。遍历的时候,
#如果发现重复元素,则比较其下标的差值是否小于k,
#如果小于则可直接返回True,否则更新字典中该键的值为新的下标
class Solution(object):
    def containsNearbyDuplicate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
       
        num_map={}
        for i in xrange(len(nums)):
            if nums[i] in num_map and i-num_map[nums[i]]<=k:
                return True
            else:
                num_map[nums[i]]=i
        
        return False

sol=Solution()
print sol.containsNearbyDuplicate([1,0,1,1], 1)

【leetcode❤python】 219. Contains Duplicate II的更多相关文章

  1. 【leetcode❤python】217. Contains Duplicate

    #-*- coding: UTF-8 -*- class Solution(object):    def containsDuplicate(self, nums):        numsdic= ...

  2. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  3. 【LeetCode】219. Contains Duplicate II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...

  4. 【一天一道LeetCode】#219. Contains Duplicate II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【LeetCode】219. Contains Duplicate II

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  6. 【leetcode❤python】350. Intersection of Two Arrays II

    #-*- coding: UTF-8 -*- class Solution(object):    def intersect(self, nums1, nums2):                ...

  7. 【leetcode❤python】119. Pascal's Triangle II

    #-*- coding: UTF-8 -*-#杨辉三角返回给定行#方法:自上而下考虑问题,从给定的一行计算出下一行,则给定行数之后,计算的最后一行就是求解的最后一行class Solution(obj ...

  8. 【leetcode❤python】107. Binary Tree Level Order Traversal II

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  9. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

随机推荐

  1. 夺命雷公狗---DEDECMS----30dedecms数据dede_archives主表进行查询l操作

    在plus目录下编写一个test2.php的文件,取出dede_archives的所有信息 <?php //编写test2.php这个文件,主要是为了实现可以取出dede_archives表的所 ...

  2. Power Gating的设计(架构)

    switching network的层次: 一般选择flatted的形式,hierarchy的结构对voltage drop和performance delay有影响. Power network的结 ...

  3. 目前主流的国产WebApp框架

    1.FrozenUI:QQ 2.WeUI:WeChat 3.SUI:Taobao

  4. 安装SQL Server 2005

    在安装SQL Server 2005时,经常会遇到一些错误,从而使系统无法正常安装.下面讲解在安装过程中经常出现的一些错误及其解决的方法.1.解决在安装SQL Server 2005时安装程序被挂起的 ...

  5. JSP中文乱码问题解决方法小结

    在使用JSP的过程中,最使人头疼的一个问题就是中文乱码问题,以下是我在软件开发中遇到的乱 码问题以及解决方法. 1.JSP页面乱码 这种乱码的原因是应为没有在页面里指定使用的字符集编码,解决方法:只要 ...

  6. std::map 自定义排序

    PS:开发中难免会用到快速检索的数据结构-map , 很多时候map自身提供的排序不能满足我们的需要或者不支持我们自定的数据结构的排序,解决办法就是自己实现排序. 这里的小案例是:我们要经用户的has ...

  7. cpu进程调度---RT Throttling【转】

    转自:http://book.2cto.com/201302/16291.html RT Throttling是对分配给实时进程的CPU时间进行限制的功能.使用实时调度策略的进程由于bug等出现不可控 ...

  8. nexus启动不了

    nexus一分钟前还能正常启动,突然就启动不了,看了下启动文件,变成0KB就知道什么问题了,文件被系统洗了. 解决方案:重新解压缩文件包里复制一个过来.

  9. Hibernate,JPA注解@OneToMany_Set

    用例代码如下: 数据库DDL语句 1,CAT表 create table CAT ( id CHAR) not null, create_time ), update_time ), cat_name ...

  10. C#中的属性————只谈属性

    废话少说直接一剑封喉--属性是对私有字段的保护(其实是对私有字段引用的另外一种变相公开化),属性在没有任何操作的时候是无法看出其优势来,上例子 // Field used by property.pr ...