题意:如果有两个相同的元素,它们之间的距离不超过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. 剑指offer--面试题15--相关

    感受:清晰的思路是最重要的!!! 题目:求链表中间节点 ListNode* MidNodeInList(ListNode* pHead) { if(pHead == NULL) return NULL ...

  2. Eclipse plugin插件开发 NoClassDefFoundError

    Eclipse的每一个plugin都有属于自己的类加载器,这是OSGI架构的基础,每一个plugin项目都是一个bundle,独立运行在各自的运行环境里面,这就造成了开发时和运行时的不同. Eclip ...

  3. Maven开源中国镜像

      mirrors> <mirror> <id>nexus-osc</id> <mirrorOf>central</mirrorOf> ...

  4. Eclipse改变外观,护眼模式

    1.Eclipse改变背景颜色 Windows menu --> Preference General -> Editors -> Text Editors(click),  在右下 ...

  5. 【leetcode】Find Peak Element ☆

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  6. ubuntu下安装spark1.4.0

    构建在hadoop2.6.0之上的 1.在官网下载spark-1.4.0-bin-hadoop2.6.tgz 2.解压到你想要放的文件夹里,tar zxvf spark-1.4.0-bin-hadoo ...

  7. 嘿嘿,JAVA里第一次运行单元测试成功,立存

    按书上写的单元测试. 居然一次过,爽!!! package org.smart4j.chapter2.test; import java.util.HashMap; import java.util. ...

  8. UIkit的confirm,好看点

    一,官方推荐的样码. <button type="button" class="uk-button" onclick="UIkit.modal. ...

  9. 【Linux高频命令专题(17)】head

    概述 head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 命令格式 hea ...

  10. SpringMVC学习总结(二)——DispatcherServlet详解

    摘要: DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring ...