forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbhyun-kim.py
More file actions
39 lines (27 loc) · 960 Bytes
/
bhyun-kim.py
File metadata and controls
39 lines (27 loc) · 960 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
"""
Solution
Algorithm:
1. Create a hashmap to store the index of each element.
2. Iterate through the list.
3. Check if the remaining value is in the hashmap.
4. If it is, return the current index and the index of the remaining value in the hashmap.
Time complexity: O(n)
Space complexity: O(n)
"""
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap = {}
for i in range(len(nums)):
remaining = target - nums[i]
if remaining in hashmap:
return [i, hashmap[remaining]]
hashmap[nums[i]] = i
def main():
test_cases = [[2, 7, 11, 15], 9, [0, 1], [3, 2, 4], 6, [1, 2], [3, 3], 6, [0, 1]]
s = Solution()
for test_case in test_cases:
nums_input, target_input, expected = test_case
assert sorted(s.twoSum(nums_input, target_input)) == expected
if __name__ == "__main__":
main()