leetcode解题报告(19):Contains Duplicate II
描述
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 absolute difference between i and j is at most k.
分析
我的想法是:遍历该数组,并判断当前元素之后有没有和该元素相等的元素,如果有,就通过distance得到这两个值之间的长度,如果小于等于k,则返回true,否则继续遍历。
代码如下:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if(nums.size() == 0)return false;
if(k <= 0)return false;
for(auto it = nums.begin(); it != nums.end(); ++it){
if(it + 1 == nums.end())return false;
auto f = find(it + 1,nums.end(),*it);
if(f != nums.end()){
if(distance(it,f) <= k)return true;
}
}
return false;
}
};
然而测试用例中最后一个数组超时了,没ac,想了一会儿后还是不知道怎么优化这个解法,于是看了下讨论区:
https://discuss.leetcode.com/topic/15012/share-my-easy-to-understand-c-code
代码看到就懂了,还是用unordered_map,以元素值为key,元素下标为value。 遍历该数组,如果该元素不在数组中,就把元素放到这个unordered_map里。否则,判断当前元素下标与map中该元素的下标之差是否小于等于k,如果是,返回true。
代码如下:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if(nums.size() == 0)return false;
unordered_map<int,int>m;
for(int i = 0; i != nums.size(); ++i{
if(m.find(nums[i]) != m.end() && i - m[nums[i]] <= k)
return true;
m[nums[i]] = i;
}
return false;
}
};
我的解法和上述解法在复杂度上相比,主要是多了distance这个函数。不过影响复杂度的主要因素是find函数。
为了查找一个范围内的值,我调用的是stl里的find函数,而这个算法的性能会略低于map自己的find函数。此外,unordered_map的一些操作的复杂度也确实低于vector。
unordered_map大法好~
leetcode解题报告(19):Contains Duplicate II的更多相关文章
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings
1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...
- LeetCode解题报告—— Permutations & Permutations II & Rotate Image
1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...
随机推荐
- Go语言学习笔记(10)——错误处理示例
// 定义一个 DivideError 结构 type DivideError struct { dividee int divider int } // 实现 `error` 接口 func (de ...
- 在vue中使用ElementUI
完整引用ElementUI: 安装:在需要使用到的vue项目目录下,使用npm下载安装: npm/cnpm i element-ui -S/--save <!-- 引入样式 --> < ...
- netty--处理器
编解码器的基类: 编码:MessageToByteEncode 解码:ByteToMessageDecoder
- Hystrix 用法及注解用法
一.hystrix参数使用方法 通过注解@HystrixCommand的commandProperties去配置,如下就是hystrix命令超时时间命令执行超时时间,为1000ms和执行是不启用超时 ...
- shell 学习笔记4-shell内置变量命令
一.shell 的一些内置命令 常用的一内部命令有:echo.eval.exec.export.read.shift 1.echo命令-在屏幕中输出信息 1)说明 格式:echo args #< ...
- 手动实现自己的spring事务注解
spring事务是基于同一个数据连接来实现的,认识到这一点是spring事务的关键,spring事务的关键点便在于在事务中不管执行几次db操作,始终使用的是同一个数据库连接.通过查看源码,我们可以看到 ...
- 在论坛中出现的比较难的sql问题:34(递归 获取连续值问题)
原文:在论坛中出现的比较难的sql问题:34(递归 获取连续值问题) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
- angular中控制器之间传递参数的方式
在angular中,每个controller(控制器)都会有自己的$scope,通过为这个对象添加属性赋值,就可以将数据传递给模板进行渲染,每个$scope只会在自己控制器内起作用,而有时候需要用到其 ...
- Java 之 字节输出流[OutputStream]
一.字节输出流 java.io.OutputStream 抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地. 该类中定义了字节输出流的基本共性功能方法. 公性方法: public v ...
- Django 使用form组件对文件上传
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...