-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContains_Dulpicate.py
More file actions
41 lines (27 loc) · 921 Bytes
/
Contains_Dulpicate.py
File metadata and controls
41 lines (27 loc) · 921 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
# With While Loop
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
left, right = 0, 0
seen = set()
while right < len(nums):
if right - left > k:
seen.remove(nums[left])
left += 1
if nums[right] in seen:
return True
seen.add(nums[right])
right += 1
return False
# With For Loop
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
left, right = 0, 0
seen = set()
for right in range(len(nums)):
if right - left > k:
seen.remove(nums[left])
left += 1
if nums[right] in seen:
return True
seen.add(nums[right])
return False