219. Contains Duplicate II
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 difference between i and jis at most k.
解题思路:
用一个表,记录扫描过程中,每个数字出现的最近
一次位置,则当前位置和最近位置的差小于k时 返回true,
bool containsNearbyDuplicate(vector<int>& nums, int k) {int size=nums.size();if(size<=1||k<=0)return false;unordered_map<int,int> table;for(int i=0;i<size;++i){auto it=table.find(nums[i]);if(it==table.end())table[nums[i]]=i;else{if(i-table[nums[i]]<=k)return true;it->second=i;}}return false;}
转载于:https://blog.51cto.com/searchcoding/1751329
219. Contains Duplicate II相关推荐
- Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- leetcode python3 简单题219. Contains Duplicate II
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百一十九题 (1)题目 英文: Given an array of intege ...
- Contains Duplicate II
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
- LeetCode Contains Duplicate II(hash)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- C#LeetCode刷题之#219-存在重复元素 II(Contains Duplicate II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3704 访问. 给定一个整数数组和一个整数 k,判断数组中是否存在 ...
- Contains Duplicate II - LeetCode
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- Python JAVA Solutions for Leetcode
Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...
- Leetcode重点250题
LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...
- LeetCode 简单算法题
使用Nodejs 抓取的LeetCode 简单算法题 一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...
最新文章
- angular ngoninit 刷新html页面_web前端入门到实战:实现html页面自动刷新
- wp7开发第一课:软件生命周期(其一)
- Python学习笔记 之 变量进阶
- Swift设计模式: 观察者模式(发布-订阅模式)
- SONY图像传感器列表
- 解决Steam绑定手机令牌的问题
- 关于嵌入式的bin、hex、axf、map
- HTTP Live Streaming (HLS) - 概念
- python爬虫新浪微博评论、评论人信息
- 太极图(HTML+CSS),无动态效果
- 视频处理指定颜色的提取
- [HITICS]大作业——程序人生Hello‘s P2P
- 第二十一章 : 文本处理
- 基于深度神经网络的高光谱影响分类方法研究---MNF+自动编码器+Softmax (准确率比较低,17年的论文)
- java通过输入的方式给int数组赋值并输出数组
- 解决zeal文档下载不上的办法
- 别用Docker了,这7大开源容器化工具也能满足你!
- InfiniBand技术和协议架构分析
- Android 加密算法
- 泛型 super和extend