Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 77266ba

Browse files
python: add problem 219 and unittest
1 parent 661d3c0 commit 77266ba

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Python/sln_201_300/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
from typing import List
3+
4+
5+
class Solution_211_220(object):
6+
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
7+
"""
8+
219
9+
:param nums:
10+
:param k:
11+
:return:
12+
"""
13+
num2idx = {}
14+
for idx, num in enumerate(nums):
15+
if num in num2idx and idx - num2idx[num] <= k:
16+
return True
17+
num2idx[num] = idx
18+
19+
return False
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest import TestCase
3+
from sln_201_300.solution_211_220 import Solution_211_220
4+
5+
6+
class Test_Solution_211_220(TestCase):
7+
def setUp(self) -> None:
8+
self.sln = Solution_211_220()
9+
10+
def test_containsNearbyDuplicate(self):
11+
nums = [1, 2, 3, 1]
12+
self.assertEqual(self.sln.containsNearbyDuplicate(nums, 3), True)
13+
self.assertEqual(self.sln.containsNearbyDuplicate([1, 0, 1, 1], 1), True)
14+
self.assertEqual(self.sln.containsNearbyDuplicate([1, 2, 3, 1, 2, 3], 2), False)

0 commit comments

Comments
 (0)