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.

哈希表的使用

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
map<int,int> value;
for(int i=0;i<nums.size();++i)
{
if(value.find(nums[i])!=value.end() && i-value[nums[i]]<=k)
return true;
value[nums[i]]=i;
}
return false;
}
};

leetCode(28):Contains Duplicate II的更多相关文章

  1. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

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

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

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

  3. python leetcode 日记 --Contains Duplicate II --219

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

  4. LeetCode 219. Contains Duplicate II (包含重复项之二)

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

  5. LeetCode 219 Contains Duplicate II

    Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...

  6. Java for LeetCode 219 Contains Duplicate II

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

  7. Leetcode 219 Contains Duplicate II STL

    找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...

  8. (easy)LeetCode 219.Contains Duplicate II

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

  9. Java [Leetcode 219]Contains Duplicate II

    题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...

随机推荐

  1. webstorm自动压缩js、css、html【工具篇】

    *注意:自动压缩的文件只能在同级目录下,不能指定文件夹,强制了文件自动保存,设置的手动保存将失效. 插件下载地址:点击这里下载 密码:e6bk 使用方法: 1.css&js 分别添加这两个,c ...

  2. Python教程(一)Python简介

    Python就为我们提供了非常完善的基础代码库,覆盖了网络.文件.GUI.数据库.文本等大量内容,被形象地称作“内置电池(batteries included)”.用Python开发,许多功能不必从零 ...

  3. OOD沉思录 --- 导引

    一个对象一定会有如下4个属性: 1,它的身份标示,可能只是它在内存中的地址; 2,它的类的属性(通常是静态属性)和这些属性的值(通常是动态的); 3,它的类的行为(从实现者的角度看); 3,它的公开接 ...

  4. NOIP2013 D1 T3 货车运输

    好吧,遇上这种题,作为蒟蒻的我第一个想到的就是怎么打暴力,然而暴力都打不好QAQ!!!于是只能等教练讲解以后,然后在大犇的指导下终于做出来了. 对了,,好像还,没上题....: 题目描述 A 国有 n ...

  5. 外行人都能看懂的SpringCloud

    一.前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringCloud的一些基础的 ...

  6. javascript面向对象思想

    JavaScript 使用函数来定义类.语法:function className(){    // 具体操作} function Person() { this.name=" 张三 &qu ...

  7. RxSwift 系列(二)

    前言 Subject是一个代理,它既是Observer,也是Observable.因为它是一个Observer,它可以订阅一个或多个Observable;因为它是一个Observable,它又可以被其 ...

  8. FastReport.Net使用:[28]数据合并

    基础数据 1.拖动数据源中的数据列到报表设计器中,获得一张简单的报表. 2.下面使用两种方法将期中考试和期末考试的成绩合并到一行显示 合并数据(分组方法) 1.按学生名字和科目来进行分组,成绩文本框咱 ...

  9. redis_NoSql入门概述数据模型简介

    以下面的背景去对比关系型数据库和非关系型数据库的差异(一个电商客户.订单.订购.地址模型来对比以下关系型数据库和非关系型数据库) 传统数据库一般设计会使用ER图(1:1/1:N/N:N,主键等) 而N ...

  10. 【BZOJ 2144】 2144: 跳跳棋 (倍增LCA)

    2144: 跳跳棋 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 642  Solved: 307 Description 跳跳棋是在一条数轴上进行的 ...