Leetcode:219. Contains Duplicate II(数组是否含有重复元素)
2017-12-06 19:11
609 查看
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.
这个题以前做过类似的——->:点击这里
多了一个限制条件就是找到最近的两个相同元素并且这两个元素的角标的绝对值之差还要小于给定K。
注意:最近最近最近
注意:最近最近最近
注意:最近最近最近
注意:最近最近最近
注意:最近最近最近
改进,关键是让index移动起来,
解法讲解:
采用滑动窗口的方法来进行求解,滑动窗口的大小为k,用HashSet来存储该窗口内的值,同时用来判断窗口内的数字是否重复出现。同时使用两个指针left和right标记窗口的两端,刚开始值都为0,然后对right不断加1,将right指向的值加入HashSet中去,判断是否出现重复值,直到righ-left>k,left加1,并移除相应的元素。如果到数组末尾没有发现重复的值则返回false。
这个题以前做过类似的——->:点击这里
多了一个限制条件就是找到最近的两个相同元素并且这两个元素的角标的绝对值之差还要小于给定K。
注意:最近最近最近
注意:最近最近最近
注意:最近最近最近
注意:最近最近最近
注意:最近最近最近
我的错误答案:
我的错误思路是只考虑最开始的两个的重复数字 比如1 0 1 1 我算出来的index差值为2 但是给定k=1 这种情况将要返回false 但是应该返回truepublic static boolean containsNearbyDuplicate(int[] nums, int k) { if (nums.length<=1){ return false; } HashSet<Integer> hashSet = new HashSet<Integer>(); int index1 = -1; int index2 = -1; int num = 0; for (int i = 0; i < nums.length; i++) { if(hashSet.add(nums[i])==false){ index1 = i; num = nums[i]; break; } } for (int i = 0; i < nums.length; i++) { if (nums[i]==num) { index2 = i; break; } } System.out.println("1:"+index1); System.out.println("2:"+index2); if(index1!=-1&&index2!=-1&&Math.abs(index1-index2)<=k) return true; return false; }
改进,关键是让index移动起来,
解法讲解:
采用滑动窗口的方法来进行求解,滑动窗口的大小为k,用HashSet来存储该窗口内的值,同时用来判断窗口内的数字是否重复出现。同时使用两个指针left和right标记窗口的两端,刚开始值都为0,然后对right不断加1,将right指向的值加入HashSet中去,判断是否出现重复值,直到righ-left>k,left加1,并移除相应的元素。如果到数组末尾没有发现重复的值则返回false。
public boolean containsNearbyDuplicate(int[] nums, int k) { HashSet<Integer> hashSet = new HashSet<Integer>(); int left=0,right=0; if(nums.length==0) return false; for(int i=0;i<nums.length;i++){ if(hashSet.add(nums[i])==true){ hashSet.add(nums[i]); right++; }else return true; if(right - left > k){ hashSet.remove(nums[left]); left++; } } return false; }
相关文章推荐
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- 219. Contains Duplicate II (判断数组中是否有重复元素且其下标之差不超过k)
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
- Remove Duplicates from Sorted Array II 从有序数组里移除重复出现元素,最多重复出现2次 @LeetCode
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
- Leetcode刷题记—— Remove Duplicates from Sorted Array II(已排序数组移除重复元素2)
- LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2
- leetCode 219. Contains Duplicate II 数组
- [LeetCode]217. Contains Duplicate(判断数组中是否有重复元素)
- LeetCode-Remove Duplicates from Sorted Array II (删除重复元素,使重复次数最多N次)
- LeetCode 219. Contains Duplicate II(检查重复)
- LeetCode Contains Duplicate II (判断重复元素)
- 219. Contains Duplicate II 包含重复元素2
- Leetcode Remove Duplicates from Sorted List II 删除链表中重复的元素II
- LeetCode | Remove Duplicates from Sorted Array(删除有序数组的重复元素)
- leetcode:26&80 Remove Duplicates from Sorted Array 删除数组中的重复元素
- LeetCode 26. Remove Duplicates from Sorted Array(移除有序数组的重复元素)
- LeetCode-Remove Duplicates from Sorted List II-从有序链表中移除重复元素-链表操作
- 287.leetcode Find the Duplicate Number(hard)[数组 重复元素]