【一天一道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 ...
随机推荐
- [BZOJ]1052 覆盖问题(HAOI2007)
三矩形覆盖问题啊……不过听说FJOI还出过双圆覆盖问题? Description 某人在山上种了N棵小树苗.冬天来了,温度急速下降,小树苗脆弱得不堪一击,于是树主人想用一些塑料薄膜把这些小树遮盖起来, ...
- JVM回收方法区内存
很多人认为方法区(或者HotSpot虚拟机中的永久代)是没有垃圾收集的,Java虚拟机规范中确实说过可以不要求虚拟机在方法区实现垃圾收集,而且在方法区进行垃圾收集的“性价比”一般比较低:在堆中,尤其是 ...
- JS 判断是否为IP格式
<html> <head> <title><a href='http://js.zz5u.net'><u>JavaScript</u& ...
- Go 实现判断变量是否为合法数字 IsNumeric 算法
[转] http://www.syyong.com/Go/Go-to-determine-whether-the-variable-is-a-legal-digital-algorithm.html ...
- expect IDENTIFIER, actual IDENTIFIER 处理
涉及到注入数据库的报错,这是很常见的了. 但是期望IDENTIFIER,实际IDENTIFIER 的报错,你们知道是什么意思吗? 我已开始看到的时候,是mybatis报错发神经了,报错了报错.再跑一次 ...
- Python小代码_11_生成小于 n 的裴波那契数列
def fib(n): a, b = 1, 1 while a < n: print(a, end=' ') a, b = b, a + b fib(100000) #输出结果 #1 1 2 3 ...
- C#利用Attribute实现简易AOP介绍
首先看一段简单的代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } //来自UI层的调用 ...
- Oracle知识梳理(一)理论篇:基本概念和术语整理
理论篇:基本概念和术语整理 一.关系数据库 关系数据库是目前应用最为广泛的数据库系统,它采用关系数据模型作为数据的组织方式,关系数据模型由关系的数据结构,关系的操作集合和关系的完整 ...
- MAC下用homebrew安装及配置apache、php和mysql
我们用到php运行环境的时候总喜欢用集成包,其实在mac下,用homebrew也可以很快的安装这些东西,配置也很简单. homebrew homebrew是mac下的一个包安装管理工具,使用非常简单方 ...
- 解决使用BottomSheetDialog时状态栏变黑的问题
问题描述 当使用support里的design 库里的BottomSheetDialog时,在6.0的机器上当对话框弹出时系统状态栏会变黑,如下图所示: 一开始以为是我用的姿势不对,试过对style配 ...