LeetCode OJ:Contains DuplicateII(是否包含重复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 difference between i and jis at most k.
这题上上一篇博客的延伸,问的是k长的距离内有没有两个数是相等的,类似一个滑动窗口问题,方法比较简单,使用一个map记下上次数出现的位置就可以了,代码如下:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
map<int, int> ret;
int sz = nums.size();
for (int i = ; i < sz; ++i){
if (ret.find(nums[i]) != ret.end() && i - ret[nums[i]] <= k)
return true;
else
ret[nums[i]] = i;
}
return false;
}
};
java版本的代码如下所示,用的方法都是一样的:
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
for(int i = 0; i < nums.length; ++i){
if(m.containsKey(nums[i]))
if(i-m.get(nums[i]) <= k)
return true;
m.put(nums[i], i);//放在这里有两个原因,如果本来存在将index更新到最近的位置,如果不存在就将它放到map中起
}
return false;
}
}
LeetCode OJ:Contains DuplicateII(是否包含重复II)的更多相关文章
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode OJ 95. Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- <LeetCode OJ> 78 / 90 Subsets (I / II)
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)
题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...
- [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] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- KGX滚动分页源码
源码描述: 本工具采用Jquery框架,通过jquery调用ashx获取并输出数据,示例中采用测试数据,可以自行扩展为图片等等 当下流行的分页方式,鼠标滚动下拉条会自动展示下一页信息,类似瀑布流的效果 ...
- 2015/7/29 (高开,V形反转,各种指标背离——可惜没买进,填补空缺图形的心理分析)
1.李大--謝先生℡:早盘决策:如今日再次出现大幅低开 或者盘中大幅下跌可逢低 3成仓位左右分散资金做短线抄底,切记是超短 绝不追高,设置5个点止损.市场有很多名家在谈论3373点即前低点,本人告诉 ...
- Kattis - prva 【字符串】
题意 从上到下 或者 从左到右 组成的长度 >= 2 的字符串 如果遇到 # 就断掉 输出 字典序最小的那一个 思路 只要从上到下 和从左到右 分别遍历一遍,将 长度 >= 2 的字符串 ...
- iOS objc_setAssociatedObject 关联对象的学习
今天看了FDTemplateLayoutCell的源码,类别里面相当频繁使用了关联对象,做笔记!!!学套路 主要函数: void objc_setAssociatedObject(id object, ...
- iOS 视频全屏功能 学习
项目中,也写过类似"视频全屏"的功能, 前一阵子读到今日头条 的一篇技术文章,详细介绍三种旋转方法差异优劣最终择取.文章从技术角度看写的非常好,从用户角度看,也用过多家有视频功能的 ...
- Linux权限管理 ACL权限
ACL权限简介 在普通权限中,用户对文件只有三种身份ugo,分别为属主(u).属组(g)和其他人(o):每种用户身份拥有读(read).写(write)和执行(execute)三种权限.但是在实际工作 ...
- MODBUS协议 一种问答方式的通信协议
源:MODBUS协议 一种问答方式的通信协议 ModBus通信系统协议
- @MarkFan 口语练习录音 20140518 [超凡蜘蛛侠2-格温的演讲[中文]&驯龙骑士选节口语录音]
一个人看不到未来,就把握不了现在 生命中最值得珍惜的,其实并不是永恒的 正因为它会结束,使其变得弥足珍贵,而且将一去不复返 让我们谨记时间就是运气,所以不要把它浪费在别的生活上 让你的生活过得更有价值 ...
- 20145240《Java程序设计》第三周学习总结
20145240 <Java程序设计>第三周学习总结 教材学习内容总结 个人感觉第三周的学习量还是很大的,需要学习的内容更难了而且量也变多了,所以投入了更多的时间到Java的学习中去. 第 ...
- Android : 反射机制获取或设置系统属性(SystemProperties)【转】
本文转载自:https://blog.csdn.net/wei_lei/article/details/70312512 Android.os.SystemProperties 提供了获取和设置系统属 ...