Skip to content

Commit 170518e

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0219
1 parent a02fa22 commit 170518e

File tree

4 files changed

+61
-44
lines changed

4 files changed

+61
-44
lines changed

solution/0200-0299/0219.Contains Duplicate II/README.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,36 @@
3333
<!-- 这里可写当前语言的特殊实现逻辑 -->
3434

3535
```python
36-
36+
class Solution:
37+
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
38+
helper = {}
39+
for i, v in enumerate(nums):
40+
if v in helper and i - helper[v] <= k:
41+
return True
42+
helper[v] = i
43+
return False
3744
```
3845

3946
### **Java**
4047

4148
<!-- 这里可写当前语言的特殊实现逻辑 -->
4249

4350
```java
44-
51+
class Solution {
52+
public boolean containsNearbyDuplicate(int[] nums, int k) {
53+
Map<Integer, Integer> helper = new HashMap<>();
54+
for (int i = 0, n = nums.length; i < n; ++i) {
55+
if (helper.containsKey(nums[i])) {
56+
int j = helper.get(nums[i]);
57+
if (i - j <= k) {
58+
return true;
59+
}
60+
}
61+
helper.put(nums[i], i);
62+
}
63+
return false;
64+
}
65+
}
4566
```
4667

4768
### **...**

solution/0200-0299/0219.Contains Duplicate II/README_EN.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,34 @@
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
60+
helper = {}
61+
for i, v in enumerate(nums):
62+
if v in helper and i - helper[v] <= k:
63+
return True
64+
helper[v] = i
65+
return False
5966
```
6067

6168
### **Java**
6269

6370
```java
64-
71+
class Solution {
72+
public boolean containsNearbyDuplicate(int[] nums, int k) {
73+
Map<Integer, Integer> helper = new HashMap<>();
74+
for (int i = 0, n = nums.length; i < n; ++i) {
75+
if (helper.containsKey(nums[i])) {
76+
int j = helper.get(nums[i]);
77+
if (i - j <= k) {
78+
return true;
79+
}
80+
}
81+
helper.put(nums[i], i);
82+
}
83+
return false;
84+
}
85+
}
6586
```
6687

6788
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
class Solution {
22
public boolean containsNearbyDuplicate(int[] nums, int k) {
3-
if(nums == null || nums.length == 0) return false;
4-
Map<Integer, Integer> map = new HashMap<>();
5-
6-
for(int i = 0; i < nums.length; i++) {
7-
if(map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) {
8-
return true;
9-
} else {
10-
map.put(nums[i], i);
3+
Map<Integer, Integer> helper = new HashMap<>();
4+
for (int i = 0, n = nums.length; i < n; ++i) {
5+
if (helper.containsKey(nums[i])) {
6+
int j = helper.get(nums[i]);
7+
if (i - j <= k) {
8+
return true;
9+
}
1110
}
11+
helper.put(nums[i], i);
1212
}
13-
1413
return false;
1514
}
1615
}
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,8 @@
1-
'''
2-
https://leetcode.com/problems/contains-duplicate-ii/
3-
4-
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.
5-
'''
6-
# Performance
7-
'''
8-
Runtime: 96 ms, faster than 93.53% of Python3 online submissions for Contains Duplicate II.
9-
Memory Usage: 20.5 MB, less than 62.50% of Python3 online submissions for Contains Duplicate II.
10-
'''
11-
12-
# Algorithm Explained
13-
'''
14-
Create a hashmap to remember the most recent position of unique values
15-
If we found duplicate and the range is less than k, then return true
16-
Else remember that index
17-
18-
Space: O(n) with n is the number of original array
19-
Complexity: O(n)
20-
'''
211
class Solution:
22-
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
23-
pos = {}
24-
for idx, element in enumerate(nums):
25-
if element in pos and idx - pos[element] <= k:
26-
return True
27-
pos[element] = idx
28-
return False
29-
30-
31-
32-
2+
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
3+
helper = {}
4+
for i, v in enumerate(nums):
5+
if v in helper and i - helper[v] <= k:
6+
return True
7+
helper[v] = i
8+
return False

0 commit comments

Comments
 (0)