LeetCode 219 Contains Duplicate II
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的更多相关文章
- [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 ...
- 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 ...
随机推荐
- 详解 Android Activity 生命周期
从以下几个方面详细说一下Activity的生命周期: 1.回到主屏幕再打开和退出程序的时候. 2.屏幕旋转的时候. 3.打开其它的Activity的情况. 4.打开一个Layou透明的Activity ...
- 【原创】日志文件清理工具V1.0
最近公司的系统服务器经常出现磁盘空间不足的情况,登陆服务器发现原来是公司的HR系统日志造成的(插个话题:我们公司的HR系统都实施两年多了还没上线,且不说软件功能如何,服务太TMD差劲了,更可气的是软件 ...
- bootstrap-markdown编辑器引入
MarkdownAsset.php <?php namespace app\assets; use yii\web\AssetBundle; class MarkdownAsset extend ...
- 篇二:JS身份证校验
身份证校验 function identityCodeValid(code) { var city={11:"北京",12:"天津",13:"河北&q ...
- 【USACO 2.3】Cow Pedigrees(DP)
问n个结点深度为k且只有度为2或0的二叉树有多少种. dp[i][j]=dp[lk][ln]*dp[rk][j-1-ln],max(lk,rk)=i-1. http://train.usaco.org ...
- MacTalk阅读有感
MacTalk by 池建强 高手的思维境界 -贴地气 -有思想 技术普及 文字是表达人思想的载体,池老师将自己的经历,经验分享给大家,很值得大家学习,虽然我只是个初出茅庐的小菜,现在有指路明灯一样. ...
- JS写小游戏(一):游戏框架
前言 前一阵发现一个不错的网站,都是一些用html5+css+js写的小游戏,于是打算学习一番,写下这个系列博客主要是为了加深理解,当然也有一些个人感悟,如果英文好可以直接Click Here. 概述 ...
- Nginx简易配置文件(一)(静态页面及PHP页面解析)
user nobody nobody; worker_processes 4; error_log logs/error.log; pid logs/nginx.pid; events { use e ...
- Samba服务器配置
Samba服务器配置流程: (1)安装samba服务器先用#rpm -ivh samba-列出与samba有关的rpm包然后选择第一个包,用tab键补齐文件名 (2)创建新用户和其密码#useradd ...
- 11月8日上午Jquery的基础语法、选取元素、操作元素、加事件、挂事件及移除事件
jquery基础知识 1.jquery文件的引入,所有的js代码要写在下面那段代码下面. <script src="../jquery-1.11.2.min.js">& ...