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. mongodb创建、更新、删除

    1.插入操作 user = {"username":"lcq","sex":"man"} db.user.insert( ...

  2. jQuery load()和ready()

    ready与load谁先执行: 大家在面试的过程中,经常会被问到一个问题:ready与load那一个先执行,那一个后执行?答案是ready先执行,load后执行. DOM文档加载的步骤: 要想理解为什 ...

  3. context.drawImage绘制图片

    context.drawImage(img,x,y)  x,y图像起始坐标 context.drawImage(img,x,y,w,h) w,h指定图像的宽度和高度 context.drawImage ...

  4. ListView实现上拉下拉刷新加载功能

    第一步.首先在你项目中创建一个包存放支持下拉刷新和上拉加载的类:

  5. AngularJS 实战讲义笔记

    第一部分 快速上手 1.1 感受AngularJs四大核心特性(MVC, 模块化,指令系统,双向数据绑定)1.2 搭建自动化的前端开发,调试,测试环境 代码编辑工具 (sublime) 断点调试工具 ...

  6. Linux 07 故障恢复

    1. 模拟MBR扇区被破坏后的修复. MBR故障恢复: 1.备份 添加硬盘 启动操作系统: 添加硬盘: 对分区格式化: 挂载: 做备份: 破坏MBR 重启系统: 关闭虚拟机 设置光盘启动 救援模式: ...

  7. EC读书笔记系列之2:条款4 确定对象被使用前已先被初始化

    条款4:确定对象被使用前已先被初始化 记住: ★为内置对象进行手工初始化,因为C++不保证初始他们 ★构造函数最好使用初始化列表,而不要在构造函数本体内使用赋值操作.初始化列表列出的成员变量,其排列次 ...

  8. js 删除本身节点元素

    var theParent = document.getElementById("bq_ly");   theParent.parentNode.removeChild(thePa ...

  9. composer时间长了,提示需要升级,结果问题来了

    今天重新开始之前的laravel项目,结果composer提示需要升级,于是 composer selfupdate 结果提示无法连接目标库,查阅composer官网,发现之前的库地址已经启用了ssl ...

  10. C#学习日志 day 5 plus------ interface 数组及stringBuilder相关

    interface 接口interface可以理解为两个程序达成的协议. 实际就是一个留给后续开发的框架.若想继承这个interface,就必须实现interface规定的 函数及结构等.一般会以大写 ...