Skip to content

Commit 57b0483

Browse files
ybian19azl397985856
authored andcommitted
feat: 219.contains-duplicate-ii add Python3 implementation (#128)
1 parent 51ff61d commit 57b0483

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

problems/219.contains-duplicate-ii.md

+35-19
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ Output: false
3636

3737
## 代码
3838

39+
* 语言支持:JS,Python
40+
41+
Javascript Code:
42+
3943
```js
4044
/*
4145
* @lc app=leetcode id=219 lang=javascript
@@ -53,34 +57,34 @@ Output: false
5357
* Given an array of integers and an integer k, find out whether there are two
5458
* distinct indices i and j in the array such that nums[i] = nums[j] and the
5559
* absolute difference between i and j is at most k.
56-
*
57-
*
60+
*
61+
*
5862
* Example 1:
59-
*
60-
*
63+
*
64+
*
6165
* Input: nums = [1,2,3,1], k = 3
6266
* Output: true
63-
*
64-
*
65-
*
67+
*
68+
*
69+
*
6670
* Example 2:
67-
*
68-
*
71+
*
72+
*
6973
* Input: nums = [1,0,1,1], k = 1
7074
* Output: true
71-
*
72-
*
73-
*
75+
*
76+
*
77+
*
7478
* Example 3:
75-
*
76-
*
79+
*
80+
*
7781
* Input: nums = [1,2,3,1,2,3], k = 2
7882
* Output: false
79-
*
80-
*
81-
*
82-
*
83-
*
83+
*
84+
*
85+
*
86+
*
87+
*
8488
*/
8589
/**
8690
* @param {number[]} nums
@@ -100,3 +104,15 @@ var containsNearbyDuplicate = function(nums, k) {
100104
};
101105
```
102106

107+
Python Code:
108+
109+
```python
110+
class Solution:
111+
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
112+
d = {}
113+
for index, num in enumerate(nums):
114+
if num in d and index - d[num] <= k:
115+
return True
116+
d[num] = index
117+
return False
118+
```

0 commit comments

Comments
 (0)