We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8e76bd1 commit 6d65a91Copy full SHA for 6d65a91
HashMap/Sliding Window/ContainsDuplicateII.java
@@ -0,0 +1,20 @@
1
+class ContainsDuplicateII {
2
+ public boolean containsNearbyDuplicate(int[] nums, int k) {
3
+
4
+ Map<Integer, Integer> map = new TreeMap<>();
5
6
+ for(int i = 0; i<nums.length; i++){
7
+ if(!map.containsKey(nums[i])){
8
+ map.put(nums[i], i);
9
+ }
10
+ else if(map.containsKey(nums[i]) && Math.abs(map.get(nums[i]) - i) <= k){
11
+ return true;
12
13
+ else{
14
15
16
17
18
+ return false;
19
20
+}
0 commit comments