【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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 j is at most k.
(二)解题
题目大意:给定一个数组,如果数组中存在连个相等的数之间的距离小于k,则返回true,反之返回false。
解题思路:只需要找到距离小于K的两个相等的数,很容易想到采用hash表的算法。
定义一个hashmap,hashmap[i]表示i这个数最近出现的位置。
当遍历到下一个i出现时,计算i-hashmap[i],如果小于k则代表存在,反之则不存在。
具体解释看代码:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int,int> hash;//STL中的hashmap为unordered_map
int size = nums.size();
for(int i = 0 ; i < size ; i++){
if(hash.find(nums[i])!=hash.end()){//如果之前出现过nums[i]这个数,就计算距离
if(i - hash[nums[i]]<=k) return true;
}
hash[nums[i]] = i;//保存最近出现nums[i]的位置
}
return false;
}
};
【一天一道LeetCode】#219. Contains Duplicate II的更多相关文章
- [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 ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- LeetCode 219. Contains Duplicate II (包含重复项之二)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- LeetCode 219 Contains Duplicate II
Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...
- Java for LeetCode 219 Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- Leetcode 219 Contains Duplicate II STL
找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...
- (easy)LeetCode 219.Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- Java [Leetcode 219]Contains Duplicate II
题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...
- C#解leetcode 219. Contains Duplicate II
该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...
- [LeetCode] 219. Contains Duplicate II 解题思路
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
随机推荐
- hdu4729 树链剖分+二分
An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (J ...
- 2015 多校联赛 ——HDU5301(技巧)
Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...
- [poj2451]Uyuw's Concert
半平面交滴裸题,但是要求nlogn,练练手 #include<iostream> #include<cstdio> #include<cmath> #include ...
- RabbitMQ-Spring AMQP
上篇文章RabbitMQ基础入门学习了rabbitMQ一些基础的api,当然spring也在原生代码的基础上做了更多的封装,这篇文章就基于spring-rabbit,学习一下spring的实现. p. ...
- C#+HtmlAgilityPack+Dapper走一波爬虫
最近因为公司业务需要,又有机会撸winform了,这次的需求是因为公司有项目申报的这块业务,项目申报前期需要关注政府发布的相关动态信息,政府部门网站过多,人工需要一个一个网站去浏览和查阅,有时候还会遗 ...
- Requests库介绍
Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...
- C#之Action和Func的用法
以前我都是通过定义一个delegate来写委托的,但是最近看一些外国人写的源码都是用action和func方式来写,当时感觉对这很陌生所以看起源码也觉得陌生,所以我就花费时间来学习下这两种方式,然后发 ...
- mysql 拼接
SELECT RTRIM(CONCAT(belong_master_ip ,'(',host_name,')')) AS cloudIP FROM `cloud_master_cfg`
- Mysql优化--慢查询日志
Mysql 系列文章主页 =============== 默认没有开启慢查询日志功能.如果不是调优需要的话,一般不建议开启. 查看是否开启慢查询日志: SHOW VARIABLES LIKE '%sl ...
- Jenkins的关闭、重启
以前一直用从cmd进入jenkins的安装目录,执行jenkins stop/start,但是新的jenkins有更加方便功能 关闭jenkins服务 只需要在访问jenkins服务器的网址url地址 ...