Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.
不能动态维护k+1大小的map,因为若有相同元素,删除元素时会将新加入的相同大小的元素删掉,导致错误。
- class Solution {
- public:
- bool containsNearbyDuplicate(vector<int>& nums, int k) {
- map<int,int> hmap;
- int n=nums.size();
- if(n<) return false;
- for(int i=;i<n;i++)
- {
- if(hmap.count(nums[i])&&i-hmap[nums[i]]<=k)
- return true;
- else
- hmap[nums[i]]=i;
- }
- return false;
- }
- };
Contains Duplicate II的更多相关文章
- [leetcode] Contains Duplicate II
Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...
- leetcode:Contains Duplicate和Contains Duplicate II
一.Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your fun ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- Contains Duplicate,Contains Duplicate II,Contains Duplicate III
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
- 217/219. Contains Duplicate /Contains Duplicate II
原文题目: 217. Contains Duplicate 219. Contains Duplicate II 读题: 217只要找出是否有重复值, 219找出重复值,且要判断两者索引之差是否小于k ...
- [LeetCode] Contains Duplicate & Contains Duplicate II
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- [LeetCode] Contains Duplicate(II,III)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- LeetCode_219. Contains Duplicate II
219. Contains Duplicate II Easy Given an array of integers and an integer k, find out whether there ...
随机推荐
- Android实战--电话拨号器
今天跟着黑马视频建立一个android app--电话拨号器 首先新建一个android项目 activity_main_xml中的代码如下: <RelativeLayout xmlns:and ...
- Python基础(5)--字典
字典由多个键及与其对应的值构成的对组成(把键值对成为项),每个键和它的值之间用冒号(:)隔开,项之间用逗号(,)隔开,而整个字典由一对大括号括起来.空字典由两个大括号组成:{} 本文地址:http:/ ...
- Purchase购物车实例分析
源代码下载地址:http://code4app.com/ios/55655def933bf09d328b5141 此源代码从中学到以下四个知识点 第一:关于右边只有一个被选中的效果展现,左边部分代码内 ...
- NSOperation创建队列
#pragma mark - NSOperation //NSOperation不能直接进行多线程的创建,需要帮助:NSOperationQueue // 使用NSOperation的第一个子类去创建 ...
- 在XcodeGhost事件之后,获取更纯净的Xcode的方法。
正值Xcode 7正式版本的更新,IOS界就冒出了个甚至可以说成涉及国家安全的大事也不为过的事件: 也可以点击网址链接看总结的更完整的文章:众多知名 APP 都中毒了,XCodeGhost 病毒事件汇 ...
- Android 之 自动匹配字符AutoCompleteTextView
AutoCompleteTextView是自动匹配字符,当我们输入一个单词或一段话的前几个字时,就会自动为你匹配后面的内容看效果图: 下面是代码: MainActivit: package com.e ...
- QT添加程序图标及窗口图标
程序图标 材料准备 图标文件:*.ico文件,存放在源文件同一目录下,如"myapp.ico" 写入图标 向*.pro文件中,独立一行写入"RC_ICONS = *.ic ...
- 面向对象and类
类和对象: 1.什么叫类:类是一种数据结构,就好比一个模型,该模型用来表述一类事物(事物即数据和动作的结合体),用它来生产真实的物体(实例). 2.什么叫对象:睁开眼,你看到的一切的事物都是一个个的对 ...
- 17082 两个有序数序列中找第k小
17082 两个有序数序列中找第k小 时间限制:1000MS 内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题 语言: 无限制 Description 已知两个已经排好序(非减 ...
- selenium如何解决window安全验证问题
解决办法 IE浏览器: 1.使用autoit,捕捉到控件后,编写au3脚本,生成exe,然后java调用exe au3脚本 Dim $account = "username" Di ...