【一天一道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 ...
随机推荐
- UVA140 ——bandwidth(搜索)
Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the ...
- SpringBoot跨域问题解决方案
一.允许全部请求跨域许可的代码: 需要继承WebMvcConfigurerAdapter类 @Configuration public class MyWebAppConfigurer extends ...
- Git常用命令及常见问题解决
$ mkdir xxx ----创建xxx目录 $ cd learngit ----切到xxx目录下 $ pwd ----查看当前文件所在目录 $ gi ...
- C语言中#define的用法
今天整理了一些#define的用法,与大家共享! 1.简单的define定义 #define MAXTIME 1000 一个简单的MAXTIME就定义好了,它代表1000,如果在程序里面写 if(i& ...
- Linux之grep命令
概述 所有的类linux系统都会提供一个名为grep(global regular expression print,全局正则表达式输出)的搜索工具.grep命令在对一个或多个文件的内容进行基于模式的 ...
- C语言程序设计第二次作业——顺序结构
(一)改错题 1.输出带框文字:在屏幕上输出以下3行信息. 错误信息1: 错误原因:i和d位置错误 改正方法:i和d位置互换 错误信息2: 错误原因:\n后缺了一个" 改正方法:\n后加一个 ...
- java集合之HashMap源码解读
源自:jdk1.8.0_121 HashMap继承自AbstractMap,实现了Map.Cloneable.Serializable. HashMap内部是由数组.链表.红黑树实现的 变量 // 默 ...
- PTA 银行排队问题之单队列多窗口服务
假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选择时,假设顾客总是选择编号最小的窗口. 本题要求输出 ...
- 实验:利用ASMLib创建ASM磁盘
环境:RHEL 6.5 + Oracle 11.2.0.4 RAC(2 nodes) 目的:在实验环境使用ASMLib配置共享ASM磁盘,虽然我们已经不建议使用ASMLib进行绑盘,但是无奈有客户是这 ...
- 数据结构之堆Heap
1. 概述 堆(也叫优先队列),是一棵完全二叉树,它的特点是父节点的值大于(小于)两个子节点的值(分别称为大顶堆和小顶堆).它常用于管理算法执行过程中的信息,应用场景包括堆排序,优先队列等. 2. 堆 ...