Given an array of integers and an integer k, return true if and only if 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.

这道题是之前那道Contains Duplicate 包含重复值的延伸,不同之处在于那道题只要我们判断下数组中是否有重复值,而这道题限制了数组中只许有一组重复的数字,而且他们坐标差不能超过k。那么我们首先需要一个哈希表,来记录每个数字和其坐标的映射,然后我们需要一个变量d来记录第一次出现重复数字的坐标差。由于题目要求只能有一组重复的数字,所以我们在遇到重复数字时,首先判断d是否已经存了值,如果d已经有值了,说明之前有过了重复数字,则直接返回false即可。如果没有,则此时给d附上值。在网上看到有些解法在这里就直接判断d和k的关系然后返回结果了,其实这样是不对的。因为题目要求只能有一组重复数,就是说如果后面又出现了重复数,就没法继续判断了。所以正确的做法应该是扫描完整个数组后在判断,先看d有没有存入结果,如果没有,则说明没出现过重复数, 返回false即可。如果d有值,再跟k比较,返回对应的结果。OJ的test case没有包含所有的情况,比如当nums = [1, 2, 3, 1, 3], k = 3时,实际上应该返回false,但是有些返回true的算法也能通过OJ。个人认为正确的解法应该入下:

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
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;
else m[nums[i]] = i;
}
return false;
}
};

其他:

思路分析:这题比较简单,可以直接定义一个长度最大为k的滑动窗口,用一个set维护窗口内的数字判断是否出现重复,使用两个指针start和end标记滑动窗口的两端,初始都是0,然后end不断进行扩展,扫描元素判断是否出现重复元素,直到发现end-start>k, 就开始移动start,并且在set中移除对应的元素。如果以为扫描到数组末尾还没有发现重复元素,那就可以返回false。时间复杂度和空间复杂度都是O(N)。

public class Solution {public boolean containsNearbyDuplicate(int[] nums, int k) {//0842Set<Integer> appearedNum = new HashSet<Integer>();int start = 0, end = 0;for(int i = 0; i < nums.length; i++){if(!appearedNum.contains(nums[i])){appearedNum.add(nums[i]);end++;} else return true;if(end - start  > k) {appearedNum.remove(nums[start]);start++;}}return false;//0848}
}
class Solution {
public:bool containsNearbyDuplicate(vector<int>& nums, int k) {map<int, int> buf;int n = nums.size();for (int i = 0; i < n; i++){if (buf.find(nums[i]) != buf.end() && i-buf[nums[i]] <= k){return true;}else{buf[nums[i]] = i;}}return false;}
};

Contains Duplicate II相关推荐

  1. 219. Contains Duplicate II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  2. LeetCode Contains Duplicate II(hash)

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  3. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  4. C#LeetCode刷题之#219-存在重复元素 II​​​​​​​(Contains Duplicate II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3704 访问. 给定一个整数数组和一个整数 k,判断数组中是否存在 ...

  5. leetcode python3 简单题219. Contains Duplicate II

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百一十九题 (1)题目 英文: Given an array of intege ...

  6. Contains Duplicate II - LeetCode

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  7. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

  8. 每天一道LeetCode-----判断数组中是否存在两个位置上面的值相等并且下标的差小于某个值

    原题链接Contains Duplicate II 判断给定数组中是否存在i和j,使得nums[i] == nums[j]并且j - i <= k 只需要保存每个数最后出现的下标,当再次遇到这个 ...

  9. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

最新文章

  1. 三层交换机解决不同VLAN间的通信—Vecloud微云
  2. 触发一个断点_一个补丁引发的RCE: 对CVE20191208的深入分析
  3. 魅蓝android底层是什么,集体去YunOS化:魅蓝2\魅蓝Metal更换安卓底层
  4. matlab z变换离散化_MATLAB作图从入门到熟练
  5. Google Closure Complier的使用
  6. Python 基本数据类型 (二) - 字符串
  7. python autoit获取网页ajax数据_WebDriver + Python 调用AutoIt例子(实现139邮箱写信页的附件上传)...
  8. 【转】oracle回闪操作
  9. 炫酷大屏demo_那些炫酷高端的可视化大屏,是如何开发出来的?
  10. 抓住“智慧城市”的机遇
  11. 【POJ2796】Feel Good (简单单调栈)
  12. docker下编译mangoszero WOW60级服务端(一)
  13. 项目开发流程_【直播回放】房地产开发项目全流程全税种税收筹划
  14. 数显之家快讯:【SHIO世硕心语】小聪明毁大前程,所有人都应该深思!
  15. 设置数据表格中某行的颜色
  16. 北大青鸟ACCP一期云题库难题总结
  17. Crafted Item - 合成装备
  18. 为什么要用NAS网络存储服务器
  19. CCF201403-5 任务调度
  20. 从网站流量指标开始,CSDN 如何洞察运营效果异动?丨评测来了

热门文章

  1. 批量修改mp3文件的title等
  2. 程序员修炼之道-笔记
  3. 从V.C.Space抄来的
  4. [PLAYING QTP] Part2—Record
  5. Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)【系列1】...
  6. 06/05/2015
  7. 如何扩展Linux的ip_conntrack
  8. Mac OS X中MySQL 的配置文件(my.cnf)的位置
  9. Excel 向程序发送命令时出现问题 解决方法 VS
  10. 通过Redis实现分布式锁