-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethod_1.py
More file actions
42 lines (32 loc) · 886 Bytes
/
Copy pathmethod_1.py
File metadata and controls
42 lines (32 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-03-19 16:57
# @Author : cancan
# @File : method_1.py
# @Function : 存在重复元素 II
"""
Question:
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,
使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k。
Example 1:
输入: nums = [1,2,3,1], k = 3
输出: true
Example 2:
输入: nums = [1,0,1,1], k = 1
输出: true
Example 3:
输入: nums = [1,2,3,1,2,3], k = 2
输出: false
"""
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
t = {}
for i, v in enumerate(nums):
if v in t:
if i - t[v] <= k:
return True
else:
t[v] = i
else:
t[v] = i
return False