-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_Two_Sum.py
More file actions
54 lines (42 loc) · 1.49 KB
/
1_Two_Sum.py
File metadata and controls
54 lines (42 loc) · 1.49 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
1. Two Sum from Leetcode.
Given an array of integers nums and an integer target,
return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution,
and you may not use the same element twice.
You can return the answer in any order.
"""
from typing import List
class Solution:
"""Two Sum Class."""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two sum function."""
# # My solution - Approach 1: Brute Force
for i in range(0, len(nums)):
for j in range(len(nums)-1, 0, -1):
if nums[i] + nums[j] == target and not i == j:
return [i, j]
# # Approach 2: Two-pass Hash Table
# hashmap = {}
# for i in range(len(nums)):
# hashmap[nums[i]] = i
# for i in range(len(nums)):
# complement = target - nums[i]
# if complement in hashmap and hashmap[complement] != i:
# return [i, hashmap[complement]]
# # Approach 3: One-pass Hash Table
# hashmap = {}
# for i in range(len(nums)):
# complement = target - nums[i]
# if complement in hashmap:
# return [i, hashmap[complement]]
# hashmap[nums[i]] = i
def main():
"""Solution."""
solution = Solution()
nums = [4, 2, 3, 11, 9, 1]
target = 3
answer = solution.twoSum(nums, target)
print(answer)
if __name__ == "__main__":
main()