描述

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 absolute difference between i and j is at most k.

分析

我的想法是:遍历该数组,并判断当前元素之后有没有和该元素相等的元素,如果有,就通过distance得到这两个值之间的长度,如果小于等于k,则返回true,否则继续遍历。

代码如下:

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if(nums.size() == 0)return false;
if(k <= 0)return false;
for(auto it = nums.begin(); it != nums.end(); ++it){
if(it + 1 == nums.end())return false;
auto f = find(it + 1,nums.end(),*it);
if(f != nums.end()){
if(distance(it,f) <= k)return true;
}
}
return false;
}
};

然而测试用例中最后一个数组超时了,没ac,想了一会儿后还是不知道怎么优化这个解法,于是看了下讨论区:

https://discuss.leetcode.com/topic/15012/share-my-easy-to-understand-c-code

代码看到就懂了,还是用unordered_map,以元素值为key,元素下标为value。 遍历该数组,如果该元素不在数组中,就把元素放到这个unordered_map里。否则,判断当前元素下标与map中该元素的下标之差是否小于等于k,如果是,返回true。

代码如下:

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

我的解法和上述解法在复杂度上相比,主要是多了distance这个函数。不过影响复杂度的主要因素是find函数。

为了查找一个范围内的值,我调用的是stl里的find函数,而这个算法的性能会略低于map自己的find函数。此外,unordered_map的一些操作的复杂度也确实低于vector。

unordered_map大法好~

leetcode解题报告(19):Contains Duplicate II的更多相关文章

  1. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  2. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  3. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

  4. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  5. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  6. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  7. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  8. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  9. LeetCode解题报告—— Permutations & Permutations II & Rotate Image

    1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...

随机推荐

  1. 12.Scratch编程小游戏——天上掉馅饼

    最意想天开的事,就是天降我们喜欢的食物,今天我们就来编写一个接馅饼的小游戏. 游戏规划: 1.用鼠标控制小猫的来回移动 2.甜甜圈从天而降 3.小猫接到绿色的甜甜圈减一分,接到红色的甜甜圈加一分,接到 ...

  2. mybatis执行DDL语句

    对MyBatis一直停留在仅仅会用的阶段,常用的场景就是通过MyBatis对表数据进行DML(insert, delete, update等)操作,从来没有想过通过MyBatis对数据库进行DDL(c ...

  3. python之基础总结(飞机大战)

    一.学习python有一段时间了,总体上手还是挺好的,但是有些东西还是和Java存在着一定的区别,这里主要是通过学习,然后自己去编写一个案例.从中学习到的一些东西,这里分享出来,如果存在不正确的地方还 ...

  4. ABP 基于DDD的.NET开发框架 学习(一)

    ABP总体介绍 ABP是ASP.NET Boilerplate Project,ASP.NET样板项目. ABP框架定位于快速开发 ABP是一个用于最快实践和流行开发现代Web应用程序的新起点,旨在成 ...

  5. win10+pyspark+pycharm+anaconda单机环境搭建

    一.工具准备 1. jdk1.8 2. scala 3. anaconda3 4. spark-2.3.1-bin-hadoop2.7 5. hadoop-2.8.3 6. winutils 7. p ...

  6. [NOIP11.1模拟赛]补番报告

    Preface 昨天开始补某科学的超电磁炮S 感觉今天就好了点,炮姐赛高 T1 一开始一直想欧拉定理&ex欧拉定理,结果估计70分,数组开小了GG,看了正解发现是我学傻了 T2 一看就是数据结 ...

  7. 数据结构之链表(LinkedList)(二)

    数据结构之链表(LinkedList)(一) 双链表 上一篇讲述了单链表是通过next 指向下一个节点,那么双链表就是指不止可以顺序指向下一个节点,还可以通过prior域逆序指向上一个节点 示意图: ...

  8. 正则表达式字符&使用

    正则详细解说:https://juejin.im/post/5965943ff265da6c30653879 一.正则表达式中的字符含意 \ 做为转义,即通常在"\"后面的字符不按 ...

  9. DBShop后台RCE之曲线救国

    本文最早发布在朋友的公众号 黑客信徒 中,文章是自己写的 不存在抄袭  特此申明 --------------------- 前言 DBShop是一款基于ZendFramework2框架的电子商务系统 ...

  10. jquery sortable的拖动方法示例详解1

    转自:https://www.jb51.net/article/45803.htm 所有的事件回调函数都有两个参数:event和ui,浏览器自有event对象,和经过封装的ui对象 ui.helper ...