题意:如果有两个相同的元素,它们之间的距离不超过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. Educational Codeforces Round 13 E. Another Sith Tournament 概率dp+状压

    题目链接: 题目 E. Another Sith Tournament time limit per test2.5 seconds memory limit per test256 megabyte ...

  2. STL中的stack(堆栈)

    转载:http://blog.csdn.net/morewindows/article/details/6950881 栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(F ...

  3. PAT-乙级-1046. 划拳(15)

    1046. 划拳(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 划拳是古老中国酒文化的一个有趣的组成部分 ...

  4. codeforces 397B

    #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...

  5. Unity3D之Vector3.Dot和Vector3.Cross的使用

    在unity3d中,Vector3.Dot表示求两个向量的点积;Vector3.Cross表示求两个向量的叉积. 点积计算的结果为数值,而叉积计算的结果为向量.两者要注意区别开来. 在几何数学中: 1 ...

  6. 【leetcode】Find Peak Element ☆

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

  7. String及其他

    String 以下例开始讲解String public class StringDemo { public static void main(String[] args) { // String s ...

  8. Android ActionBar下拉选项

    package com.example.actionBarTest.actionBarList; import android.app.ActionBar; import android.app.Ac ...

  9. Thread的第二天学习

    1.Timer 2.TimerTask 3.new Timer().schedule(new TimerTask(    public void run(){...}    ),long/date [ ...

  10. c# 在windows服务中 使用定时器

    由于最近做自动执行的程序,开始做windows服务程序, 在windows服务中如何使用定时器的时候一直失效, 以前是直接拖入timer控件,但是不能直接运行,后来在网上找了一段程序,好使了. //开 ...