题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false。

思路:用map记录每个出现过的最近的位置,扫一边序列即可。扫到一个元素就判断它在前面什么地方出现过。本题数据有点弱。

 class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if(nums.empty()) return false;
unordered_map<int,int> mapp;
nums.insert(nums.begin(),);
for(int i=; i<nums.size(); i++){
if(!mapp[nums[i]]) mapp[nums[i]]=i; //第一次出现
else{
if(i-mapp[nums[i]]<=k ) return true; //已经出现,且符合条件
mapp[nums[i]]=i; //不符合,更新最近一个位置
}
}
return false;
}
};

AC代码

LeetCode Contains Duplicate II (判断重复元素)的更多相关文章

  1. LeetCode Contains Duplicate (判断重复元素)

    题意: 如果所给序列的元素不是唯一的,则返回true,否则false. 思路: 哈希map解决. class Solution { public: bool containsDuplicate(vec ...

  2. [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 ...

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

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

  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] 220. Contains Duplicate III 包含重复元素 III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  6. [LeetCode] Contains Duplicate III 包含重复值之三

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  7. 【python】Leetcode每日一题-存在重复元素3

    [python]Leetcode每日一题-存在重复元素3 [题目描述] 给你一个整数数组 nums 和两个整数 k 和 t .请你判断是否存在 两个不同下标 i 和 j,使得 abs(nums[i] ...

  8. [leetcode] Contains Duplicate II

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

  9. lintcode 中等题:subsets II 带重复元素的子集

    题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...

  10. leetcode第217.题存在重复元素

    1.题目描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 2.示例 2.1 输入: [1,2,3,1 ...

随机推荐

  1. 用matlab查找txt文档中的关键字,并把关键字后面的数据存到起来用matlab处理

    用matlab查找txt文档中的关键字,并把关键字后面的数据存到起来用matlab处理 我测了一组数据存到txt文件中,是个WIFI信号强度文档,里面有我们需要得到的数据,有没用的数据,想用matla ...

  2. Bad configuration option localCommand

     command-line: line 0: Bad configuration option: PermitLocalCommand 2011-12-08 14:04:54 标签:Bad confi ...

  3. SQL中的事物【转】

    来源于:http://www.cnblogs.com/zhuifengnianshao/archive/2010/11/24/1886939.html 事务(Transaction)是并发控制的单位, ...

  4. hdu 3923 Invoker

    完全是套用polya模版…… ;}

  5. 【转载】关于ActionContext.getContext().getParameters()获值问题

    ActionContext.getContext().getParameters():一个学员问题的解答 2012-11-12 15:12:05|  分类: 默认分类 |  标签:struts2   ...

  6. 李洪强漫谈iOS开发[C语言-007]-语言标准简介

    C语言是介于低级语言和高级语言之间的 一个应用程序 C语言在嵌入式上使用,的确是具有低级语言的特征 直接操作硬件,扫描内存 访问到的都是虚拟内存,一个应用程序占多大内存? 表示最多 可以放多少条指令 ...

  7. Qt4升级Qt5注意问题

    Qt4升级Qt5注意问题 Qt4过渡到Qt5的项目一开始就受阻,记录一下遇到的下面的问题 --->编译遇到类似错误: error: QCalendarWidget: No such file o ...

  8. 开发版本控制git

    git init 在git命令行中依次输入 touch readme.txt并回车, git add . 点代表所有, git commit -m "init first"并回车, ...

  9. JavaWeb项目开发案例精粹-第2章投票系统-003Dao层

    1. package com.sanqing.dao; import java.util.List; import com.sanqing.bean.Vote; import com.sanqing. ...

  10. java:装饰者模式,节点流和处理流

    接口: interface Worker { public void dowork(); } 清洁工:被装饰者,节点流 class Clean implements Worker { public v ...