Skip to content

Commit

Permalink
feat: 219.contains-duplicate-ii add Python3 implementation (azl397985…
Browse files Browse the repository at this point in the history
  • Loading branch information
ybian19 authored and azl397985856 committed Aug 20, 2019
1 parent 51ff61d commit 57b0483
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions problems/219.contains-duplicate-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Output: false

## 代码

* 语言支持:JS,Python

Javascript Code:

```js
/*
* @lc app=leetcode id=219 lang=javascript
Expand All @@ -53,34 +57,34 @@ Output: false
* 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.
*
*
*
*
* Example 1:
*
*
*
*
* Input: nums = [1,2,3,1], k = 3
* Output: true
*
*
*
*
*
*
* Example 2:
*
*
*
*
* Input: nums = [1,0,1,1], k = 1
* Output: true
*
*
*
*
*
*
* Example 3:
*
*
*
*
* Input: nums = [1,2,3,1,2,3], k = 2
* Output: false
*
*
*
*
*
*
*
*
*
*
*/
/**
* @param {number[]} nums
Expand All @@ -100,3 +104,15 @@ var containsNearbyDuplicate = function(nums, k) {
};
```

Python Code:

```python
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
d = {}
for index, num in enumerate(nums):
if num in d and index - d[num] <= k:
return True
d[num] = index
return False
```

0 comments on commit 57b0483

Please sign in to comment.