-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp219.py
54 lines (41 loc) · 1.61 KB
/
p219.py
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
43
44
45
46
47
48
49
50
51
52
53
54
import unittest
from typing import List
# 用哈希表判断是否遇到重复,重复时检查下标距离
class Solution1:
@staticmethod
def containsNearbyDuplicate(nums: List[int], k: int) -> bool:
# 元素 => 下标
hashmap = dict()
for i in range(len(nums)):
num = nums[i]
if num in hashmap and i - hashmap[num] <= k:
return True
# 无论num在不在hashmap中,都更新下标
hashmap[num] = i
return False
# 题意是窗口内是否包括重复数字,维护窗口的哈希表就行了
class Solution2:
@staticmethod
def containsNearbyDuplicate(nums: List[int], k: int) -> bool:
# set记录窗口内出现过的元素
hashset = set()
w = k + 1 # 最大窗口是k+1,如果比这个窗口大,移除元素
for i in range(len(nums)):
if i >= w:
hashset.remove(nums[i - w])
# 新进的元素是否在set中,是,返回True;否,加入set
if nums[i] in hashset:
return True
else:
hashset.add(nums[i])
return False
class Test(unittest.TestCase):
def test(self) -> None:
self.assertEqual(Solution2.containsNearbyDuplicate([1, 2, 3, 1], 3), True)
self.assertEqual(Solution2.containsNearbyDuplicate([1, 0, 1, 1], 1), True)
self.assertEqual(
Solution2.containsNearbyDuplicate([1, 2, 3, 1, 2, 3], 2), False
)
self.assertEqual(Solution2.containsNearbyDuplicate([99, 99], 2), True)
if __name__ == "__main__":
unittest.main()