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. ASP.NET设置焦点到输入框

    Page.SetFocus(txtLoginName);

  2. javascript高级知识点——实例类型

    代码信息来自于http://ejohn.org/apps/learn/. 分析一下对象的结构 function Ninja(){} var ninja = new Ninja(); console.l ...

  3. ORA-32001: 已请求写入 SPFILE, 但是在启动时未指定 SPFILE

    SQL> alter system set smtp_out_server='smtp.126.com' scope=both;alter system set smtp_out_server= ...

  4. unity3d实现Socket

    首先创建一个服务器 using UnityEngine; using System.Collections; using System.Net.Sockets; using System.Net; u ...

  5. MFC网络编程

    一.概念1.同步方式与异步方式同步方式:发送方不等接收方响应,便接着发送下一个数据包的通信方式异步方式:发送方发出数据,等收到接收方发回的响应后,才发送下一个数据包的通信方式2.阻塞与非阻塞方式阻塞套 ...

  6. php消息队列

    Memcache 一般用于缓存服务.但是很多时候,比如一个消息广播系统,需要一个消息队列.直接从数据库取消息,负载往往不行.如果将整个消息队列用一个key缓存到memcache里面.对于一个很大的消息 ...

  7. c语言面试题(感觉比较好的题目)

    1.static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别? 答:static全局变量--只在定义了该变量的源文件内有效,初 ...

  8. Robot Framework中DatabaseLibrary应用

    DatabaseLibrary: 在RF的官网上,有DatabaseLibrary的下载链接,DatabaseLibrary有2个版本,Python和Java版.本人使用的是Python版本. 1.下 ...

  9. 导出excel java实现

    1.前台页面代码: <tr> <td><input dataId="excel" type="button" value=&quo ...

  10. jQuery中find()和filter()的区别

    filter(expr):筛选出与指定表达式匹配的元素的元素集合,其中expr可以是多个选择器的组合.是对自身集合元素的筛选. find()会在元素内寻找匹配元素,而filter()是筛选元素:是对它 ...