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.

class Solution:
# @param {integer[]} nums
# @param {integer} k
# @return {boolean}
def containsNearbyDuplicate(self, nums, k):
length = len( nums )
if length <= 1:
return False
if k <= 0:
return False
arrMap = {}
for i in range(0, length):
if arrMap.has_key( nums[i] ):
j = arrMap[nums[i]]
if i - j <= k:
return True
arrMap[nums[i]] = i
return False

leetcode Contains Duplicate II python的更多相关文章

  1. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  2. [leetcode] Contains Duplicate II

    Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...

  3. [LeetCode] Contains Duplicate(II,III)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  4. [LeetCode] Contains Duplicate II 包含重复值之二

    Given an array of integers and an integer k, return true if and only if there are two distinct indic ...

  5. leetcode Combination Sum II python

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  6. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  7. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  8. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  9. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

随机推荐

  1. 重写系统中的UINavigationController 返回按钮的事件

    .扩展UIviewController UIViewController+BackButtonHandler.h #import <UIKit/UIKit.h> @protocol Bac ...

  2. Tomcat7配置数据源(Oracle)

    修改../conf/content.xml <?xml version='1.0' encoding='utf-8'?> <!-- The contents of this file ...

  3. ios 中的构造方法(二)

    在之前有简单介绍了构造方法的结构,以及构造方法的作用,那么我们现在来讨论一下: 对象的创建分为两步:+ alloc 分配内存空间和 -init 进行初始化 那么在继承自 NSObject 的类当中,我 ...

  4. 2016-09-06 J2EE基础知识之不知

    1.中间件.容器.Web服务器 1.1中间件 中间件是提供系统软件和应用软件之间连接的软件,以便于软件各部件之间的沟通.中间件处于操作系统和更高一级应用程序之间. J2EE提出的背景: 1)企业级应用 ...

  5. 剑指offier第10题

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表. 二进制中1的个数   时间限制:1秒空间限制:32768K  

  6. git https连接方式,记住密码

    Git使用https方式进行连接时,默认每次推送时都要输入用户名和密码. 可以使用命令 $git config credential.helper store 为当前仓库设置记住密码,设置后,只要在推 ...

  7. 游戏基础元素之精灵——Cocos2d-x学习历程(九)

    1.创建精灵 在实际使用中,精灵是由一个纹理创建的.在不加任何设置的情况下,精灵就是一张显示在屏幕上的图片.通常精灵置于层下,因此我们首选在层的初始化方法中创建精灵,设置属性,并添加到层中. 有多种方 ...

  8. 汇编语言学习系列 for循环实现

    假如汇编语言要实现如下C语言的功能,编译环境Ubuntu14.04(32位). #include<stdio.h> int fact_for(int n) { int i; ; ; i & ...

  9. QF——iOS代理模式

    iOS的代理模式: A要完成某个功能,它可以自己完成,但有时出于一些原因,不方便自己完成.这时A可以委托B来帮其完成此功能,即由B代理完成.但是这个功能不是让B随随便便任其完成.此时,会有一个协议文件 ...

  10. css3之border-color

    -moz-border-top-colors:上边框-moz-border-right-colors:右边框-moz-border-bottom-colors:下边框-moz-border-left- ...