【leetcode❤python】 219. Contains Duplicate II
#-*- 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的更多相关文章
- 【leetcode❤python】217. Contains Duplicate
#-*- coding: UTF-8 -*- class Solution(object): def containsDuplicate(self, nums): numsdic= ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 【LeetCode】219. Contains Duplicate II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...
- 【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】219. Contains Duplicate II
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- 【leetcode❤python】350. Intersection of Two Arrays II
#-*- coding: UTF-8 -*- class Solution(object): def intersect(self, nums1, nums2): ...
- 【leetcode❤python】119. Pascal's Triangle II
#-*- coding: UTF-8 -*-#杨辉三角返回给定行#方法:自上而下考虑问题,从给定的一行计算出下一行,则给定行数之后,计算的最后一行就是求解的最后一行class Solution(obj ...
- 【leetcode❤python】107. Binary Tree Level Order Traversal II
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
随机推荐
- Logic and Fault simulation
fault simulation是指对fault circuit的simulation,来locate manufacturing defects并且进行fault diagnosis. logic ...
- linux与windows的文本文件之间的转换
在CentOS中需要安装一个软件包:tofrodos 包里包含的命令可以用包管理工具列出包里的文件. 以 CentOS 的 rpm 为例: rpm -ql tofrodos 在ArchLinux中需要 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON RegionToBin1
zw版[转发·台湾nvp系列Delphi例程]HALCON RegionToBin1 unit Unit1;interfaceuses Windows, Messages, SysUtils, Var ...
- [sinatra] Sinatra再入门
原文URL:http://www.rubycc.com/bbs/topic_detail/86 1.基础代码app.rb require 'rubygems' require 'sinatra/bas ...
- autohotkey --- win10运行不兼容
在win10下许多脚本运行有问题, 将AutoHotkey.exe设置为兼容模式为win7 同时要设置为以管理员身份运行此程序 这个必须得记录一下.
- 【python cookbook】【数据结构与算法】13.通过公共键对字典列表排序
问题:想根据一个或多个字典中的值来对列表排序 解决方案:利用operator模块中的itemgetter()函数对这类结构进行排序是非常简单的. # Sort a list of a dicts on ...
- Linux workqueue疑问【转】
转自:http://blog.csdn.net/angle_birds/article/details/9387365 各位大神,你们好.我在使用workqueue的过程中遇到一个问题. 项目采用uC ...
- css改变背景透明度【转】
兼容主流浏览器的CSS透明代码: .transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0. ...
- let和expr比较
1.空格 let:要求任何操作符两边不能含有空格 expr:必须有空格 2.运算符转义 expr:要求 let:没有要求 [xiluhua@vm-xiluhua][~]$ let * -bash: l ...
- [ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:2.3.1:jar (default-jar) on
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:2.3.1:jar (default-jar) on ...