|
| 1 | +from unittest import TestCase, main |
| 2 | +from typing import List |
| 3 | +from collections import Counter |
| 4 | + |
| 5 | + |
| 6 | +class Solution: |
| 7 | + def containsDuplicate(self, nums: List[int]) -> bool: |
| 8 | + return self.solve_3(nums=nums) |
| 9 | + |
| 10 | + """ |
| 11 | + Runtime: 412 ms (Beats 75.17%) |
| 12 | + Analyze Complexity: O(n) |
| 13 | + Memory: 31.92 MB (Beats 45.93%) |
| 14 | + """ |
| 15 | + def solve_1(self, nums: List[int]) -> bool: |
| 16 | + return len(nums) != len(set(nums)) |
| 17 | + |
| 18 | + """ |
| 19 | + Runtime: 423 ms (Beats 39.66%) |
| 20 | + Analyze Complexity: O(n) |
| 21 | + Memory: 34.54 MB (Beats 14.97%) |
| 22 | + """ |
| 23 | + def solve_2(self, nums: List[int]) -> bool: |
| 24 | + counter = {} |
| 25 | + for num in nums: |
| 26 | + if num in counter: |
| 27 | + return True |
| 28 | + else: |
| 29 | + counter[num] = True |
| 30 | + else: |
| 31 | + return False |
| 32 | + |
| 33 | + """ |
| 34 | + Runtime: 441 ms (Beats 16.59%) |
| 35 | + Analyze Complexity: O(n) |
| 36 | + Memory: 34.57 MB (Beats 14.97%) |
| 37 | + """ |
| 38 | + def solve_3(self, nums: List[int]) -> bool: |
| 39 | + return Counter(nums).most_common(1)[0][1] > 1 |
| 40 | + |
| 41 | + |
| 42 | +class _LeetCodeTCs(TestCase): |
| 43 | + def test_1(self): |
| 44 | + nums = [1, 2, 3, 1] |
| 45 | + output = True |
| 46 | + self.assertEqual(Solution.containsDuplicate(Solution(), nums=nums), output) |
| 47 | + |
| 48 | + def test_2(self): |
| 49 | + nums = [1, 2, 3, 4] |
| 50 | + output = False |
| 51 | + self.assertEqual(Solution.containsDuplicate(Solution(), nums=nums), output) |
| 52 | + |
| 53 | + def test_3(self): |
| 54 | + nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2] |
| 55 | + output = True |
| 56 | + self.assertEqual(Solution.containsDuplicate(Solution(), nums=nums), output) |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + main() |
0 commit comments