leetcode--Contains Duplicate II
来源:互联网 发布:淘宝联盟权益推广 编辑:程序博客网 时间:2024/06/06 18:38
Given an array of integers and an integer k, find out whether there there are two distinct indicesi and j in the array such that nums[i] = nums[j] and the difference betweeni and j is at most k.
题意:给定一个数组和整数k,在数组中查找是否存在两个序号i和j,使nums[i]=nums[j],并且i和j的差最多是k
分类:数组,Hash
解法1:如果两层遍历。时间复杂度是O(n^2),会超时。
和Contains Duplicate类似,我们只遍历一次数组,使用hashMap来记录遍历过的整数,key为这个整数,value为整数所在的序号
如果在遍历过程中发现了重复,比较当前序号和hashmap中的序号,判断差是否小于k,如果是,则返回真,否则继续遍历
原文链接
阅读全文
0 0
- [LeetCode]Contains Duplicate II
- [LeetCode] Contains Duplicate II
- leetcode Contains Duplicate II
- LeetCode Contains Duplicate II
- [leetcode] Contains Duplicate II
- LeetCode Contains Duplicate II
- 【leetcode】Contains Duplicate II
- leetcode--Contains Duplicate II
- [leetcode]Contains Duplicate II
- [leetCode] Contains Duplicate II
- LeetCode: Contains Duplicate II
- LeetCode Contains Duplicate II
- LeetCode | Contains Duplicate II
- LeetCode Contains Duplicate II
- 【LeetCode】Contains Duplicate II
- leetcode: Contains Duplicate II
- LeetCode Contains Duplicate II
- Leetcode: Contains Duplicate II
- Longest Ordered Subsequence【POJ 2533 dp 最长不降子序列】
- 顺序表应用2:多余元素删除之建表算法
- Install Qt 5 on Ubuntu
- csdn如何转载别人的文章
- 《cv中文参考手册-图像轮廓处理-结构分析与形状识别-获取各个对象的质心》HuMoments
- leetcode--Contains Duplicate II
- (转)eclipse 打开pom.xml文件很慢 设置pom.xml打开方式
- newFixedThreadPool
- vue axios 遇到“No 'Access-Control-Allow-Origin' header is present on the requested resource.”的问题
- 移动网络介绍
- Spring-AOP理解
- shell 内嵌 二进制
- HDU 6060 RXD and dividing
- 系统学习深度学习(三十一)--CTC