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 jis at most k.

Subscribe to see which companies asked this question

bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> hash;
int i = ;
for (auto num : nums)
{
unordered_map<int, int>::iterator iter = hash.find(num);
if (iter == hash.end())
hash.insert(make_pair(num, i));
else {
if (i - (*iter).second <= k)
return true;
(*iter).second = i;
}
++i;
}
return false;
}

Contains Duplicate II leetcode的更多相关文章

  1. 219. Contains Duplicate II - LeetCode

    Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...

  2. Contains Duplicate II ——LeetCode

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  3. [leetcode] Contains Duplicate II

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

  4. leetcode:Contains Duplicate和Contains Duplicate II

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

  5. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  6. LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II

     1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...

  7. [LeetCode] Contains Duplicate & Contains Duplicate II

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

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

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

  9. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

随机推荐

  1. Java 验证用户名、密码

    1. 数据库操作 2.验证用户 2.1. 查询 String sql = String.format("select count(*) from user where name='%s' a ...

  2. Velocity教程

    Velocity 语法(转) 一.基本语法 1."#"用来标识Velocity的脚本语句,包括#set.#if .#else.#end.#foreach.#end.#iinclud ...

  3. globalToLocal的坐标变换

    globalToLocal $(function() { init(); }); // globalToLocal var stage, holder1, holder2,shape; functio ...

  4. WPF中将16进制颜色码转换成SolidColorBrush

    使用ColorConverter.ConvertFromString(string colorValue)方法 例如:new SolidColorBrush((Color)ColorConverter ...

  5. Java8 Lumbda表达式 初步

    Java8 Lumbda表达式 初步 package com.stono.test; import java.util.function.BinaryOperator; public class Te ...

  6. libcurl模拟登录CSDN并自动评论资源以获取积分

    软件及源码下载:(http://pan.baidu.com/s/1jGE52pK) 涉及到的知识点: C++多线程编程 libcurl的使用(包括发送http请求.发送cookie给服务器.保存coo ...

  7. 笔记之《用python写网络爬虫》

    1 .3 背景调研 robots. txt Robots协议(也称为爬虫协议.机器人协议等)的全称是"网络爬虫排除标准"(Robots Exclusion Protocol),网站 ...

  8. 为什么亚马逊云计算中的DNS服务叫Route53?

    最近在用亚马逊的云计算服务,看到它的DNS服务的名字叫做"Route 53".这个名字让我很好奇,为什么叫"Route 53"呢?有什么特殊含义? 看到了这个Q ...

  9. phpcmsV9于基本介绍

    1.phpcms做企业站 2.内容+管理栏目=页面显示的导航 3.文件目录结构 根目录 api 接口文件目录 caches 缓存文件目录 confings 系统配置文件目录 caches_*系统缓存目 ...

  10. 为什么使用enable_shared_from_this——shared_ptr两类错误

    在使用C++实现弱回调时,订阅者应当维护一系列发布者的weak_ptr,而发布者注册回调时要传出this的shared_ptr指针,流行的实现方法是使用std::enable_shared_from_ ...