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 ...
随机推荐
- FineUI(专业版)v2.6.0即将支持的两个新特性!
特性1:以一挡三,将 160 行代码缩减为 60 行的技巧! 为了更新单元格的编辑值,我们需要下面三个函数同时上阵: GetModifiedDict:修改的单元格值 GetDeletedList:删除 ...
- C#扩展方法知多少
前言:上篇 序列化效率比拼——谁是最后的赢家Newtonsoft.Json 介绍了下序列化方面的知识.看过Demo的朋友可能注意到了里面就用到过泛型的扩展方法,本篇打算总结下C#扩展方法的用法.博主打 ...
- UML——在Visual Studio 2013/2015中设计UML类图
1.UML简介 Unified Modeling Language (UML)又称统一建模语言或标准建模语言. 简单说就是以图形方式表现模型,根据不同模型进行分类,在UML 2.0中有13种图,以下是 ...
- java8日期时间
###与时间有关的5个包* java.time* java.time.chrono* java.time.format* java.time.temporal* java.time.zone###ja ...
- 上传本地代码到github
第一步:建立git仓库 cd到你的本地项目根目录下,执行git命令git init第二步:将项目的所有文件添加到仓库中git add .如果想添加某个特定的文件,只需把.换成特定的文件名即可第三步:将 ...
- couldn't open file: data/coco.names
在ubuntu下配置yolo(v2)的时候,编译了源码后,尝试运行demo: ./darknet detect cfg/yolo.cfg yolo.weights data/dog.jpg 结果报错提 ...
- JAVA 技术手册 卷1 第十四章『多线程』 读书摘要
什么是线程 进程受CPU时间片的轮转调度,进而予人多任务并发的感觉. 线程在更低层次上扩展多任务概念,一个进程通常包含多个线程. 进程各自数据独立,而线程共享数据. 数据独立使进程相互通信变得繁难,共 ...
- Level Of Management Protocols - SNMP Tutorial
30.2 The Level Of Management Protocols Originally, many wide area networks included management proto ...
- 【原】理解javascript中的闭包
闭包在javascript来说是比较重要的概念,平时工作中也是用的比较多的一项技术.下来对其进行一个小小的总结 什么是闭包? 官方说法: 闭包是指有权访问另一个函数作用域中的变量的函数.创建闭包的常见 ...
- php常用字符串函数小结
php内置了98个字符串函数(除了基于正则表达式的函数,正则表达式在此不在讨论范围),能够处理字符串中能遇到的每一个方面内容,本文对常用字符串函数进行简单的小结,主要包含以下8部分:1.确定字符串长度 ...