Problem:

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.

Summary:

给出数组nums,整型数k,找到数组中是否存在nums[i]和nums[j]相等且i - j小于k的情况。

Solution:

开始用最简单写法,两重for循环逐一查找,出现TLE。

后用HashMap改进,key为数组中出现的数字,value为最近出现位置的下标。

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

LeetCode 219 Contains Duplicate II的更多相关文章

  1. [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 ...

  2. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

  3. 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 ...

  4. 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 ...

  5. Leetcode 219 Contains Duplicate II STL

    找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...

  6. (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 ...

  7. Java [Leetcode 219]Contains Duplicate II

    题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...

  8. C#解leetcode 219. Contains Duplicate II

    该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...

  9. [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 ...

随机推荐

  1. win7/8 访问 访问局域网 默认加载域 而无法成功访问的问题

    运行 gpedit.msc选择 本地计算机策略- 计算机配置-windows设置-安全设置-本地策略-安全选项 其中有个 网络安全:lan管理员身份验证级别 选择 发送LM和NTLM响应

  2. 什么是Jedis?

    Jedis 是 Redis 官方首选的 Java 客户端开发包. 实例方法: 1 import redis.clients.jedis.* 1 2 3 Jedis jedis = new Jedis( ...

  3. c# Newtonsoft.Json序列化和反序列 js解析

    Newtonsoft.Json下载地址:http://www.newtonsoft.com/products/json/ 参考:      http://www.cnblogs.com/yanweid ...

  4. windows2003安装证书服务:csp配置不正确、您没有此密钥容器的写访问权限

    1.填写CA名称后在生成密钥时提示:csp配置不正确或安装不完整.      原因:可能的原因为CS服务(Crysptographic Service)没有启动 . ps:该服务依赖RPC服务,但RP ...

  5. js-JavaScript高级程序设计学习笔记20

    第二十三章 离线应用与客户端存储 1.离线检测 HTML5定义navigator.onLine属性.还定义了两个事件,online和offline,当网络从离线变为在线或从在线变为离线时在window ...

  6. sql 知识点系统汇总

    提供性能: .服务器往往具有强大的计算能力和速度..避免把大量的数据下载到客户端,减少网络上的传输量. 第一章 T-SQL 语句 1.1数据类型 文本型 -- CHAR 定长型 <=8000字节 ...

  7. Alpha阶段第八次Scrum Meeting

    情况简述 Alpha阶段第八次Scrum Meeting 敏捷开发起始时间 2016/10/31 00:00 敏捷开发终止时间 2016/10/32 00:00 会议基本内容摘要 跟助教进行了交流,明 ...

  8. C# GUID转换成16位字符串或19位数字并确保唯一

    /// <summary> /// 根据GUID获取16位的唯一字符串 /// </summary> /// <param name=\"guid\" ...

  9. visio二次开发——图纸解析

    (转发请注明来源:http://www.cnblogs.com/EminemJK/) visio二次开发的案例或者教程,国内真的非常少,这个项目也是花了不少时间来研究visio的相关知识,困难之所以难 ...

  10. OC初步 (一)

    OC完全兼容C, 代码后缀名一般习惯用 *.m 或 *.mm,按惯例从 "Hello,World!" 开始,我们编写一个 test.mm 文件如下: #import <Fou ...