|
11 | 11 | Because nums[0] + nums[1] = 2 + 7 = 9,
|
12 | 12 | return [0, 1].
|
13 | 13 | """
|
| 14 | +from __future__ import annotations |
14 | 15 |
|
15 | 16 |
|
16 |
| -def twoSum(nums, target): |
| 17 | +def two_sum(nums: list[int], target: int) -> list[int]: |
17 | 18 | """
|
18 |
| - :type nums: List[int] |
19 |
| - :type target: int |
20 |
| - :rtype: List[int] |
| 19 | + >>> two_sum([2, 7, 11, 15], 9) |
| 20 | + [0, 1] |
| 21 | + >>> two_sum([15, 2, 11, 7], 13) |
| 22 | + [1, 2] |
| 23 | + >>> two_sum([2, 7, 11, 15], 17) |
| 24 | + [0, 3] |
| 25 | + >>> two_sum([7, 15, 11, 2], 18) |
| 26 | + [0, 2] |
| 27 | + >>> two_sum([2, 7, 11, 15], 26) |
| 28 | + [2, 3] |
| 29 | + >>> two_sum([2, 7, 11, 15], 8) |
| 30 | + [] |
| 31 | + >>> two_sum([3 * i for i in range(10)], 19) |
| 32 | + [] |
21 | 33 | """
|
22 | 34 | chk_map = {}
|
23 | 35 | for index, val in enumerate(nums):
|
24 | 36 | compl = target - val
|
25 | 37 | if compl in chk_map:
|
26 |
| - indices = [chk_map[compl], index] |
27 |
| - print(indices) |
28 |
| - return [indices] |
29 |
| - else: |
30 |
| - chk_map[val] = index |
31 |
| - return False |
| 38 | + return [chk_map[compl], index] |
| 39 | + chk_map[val] = index |
| 40 | + return [] |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + import doctest |
| 45 | + |
| 46 | + doctest.testmod() |
| 47 | + print(f"{two_sum([2, 7, 11, 15], 9) = }") |
0 commit comments