-
Notifications
You must be signed in to change notification settings - Fork 43
/
two-sum.py
154 lines (132 loc) · 4.03 KB
/
two-sum.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
1. Two Sum
Easy
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.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
Constraints:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.
Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
"""
# V0
# IDEA : HASH TABLE
# DEMO :
# In [2]: class Solution(object):
# ...: def twoSum(self, nums, target):
# ...: """
# ...: :type nums: List[int]
# ...: :type target: int
# ...: :rtype: List[int]
# ...: """
# ...: lookup = {}
# ...: for i, num in enumerate(nums):
# ...: if target - num in lookup:
# ...: return [lookup[target - num], i]
# ...: lookup[num] = i
# ...: print (lookup)
# ...:
# ...: nums = [2, 0, 100, 3,2, 9 ,7, 11, 15]
# ...: target = 9
# ...: Solution().twoSum(nums, target)
# ...:
# {2: 0}
# {2: 0, 0: 1}
# {2: 0, 0: 1, 100: 2}
# {2: 0, 0: 1, 100: 2, 3: 3}
# {2: 4, 0: 1, 100: 2, 3: 3}
# Out[2]: [1, 5]
class Solution(object):
def twoSum(self, nums, target):
lookup = {}
for i, num in enumerate(nums):
if target - num in lookup:
return [lookup[target - num], i]
lookup[num] = i
return [-1, -1]
# V0'
# IDEA : dict
class Solution(object):
def twoSum(self, nums, target):
d = {}
for i in range(len(nums)):
if target - nums[i] in d:
return [d[target - nums[i]], i]
else:
if nums[i] not in d:
d[nums[i]] = i
# V1
# https://blog.csdn.net/coder_orz/article/details/52039233
# IDEA : LINEAR SCAN
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(len(nums) - 1):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
# V1'
# https://www.jiuzhang.com/solution/two-sum/#tag-highlight-lang-python
class Solution(object):
def twoSum(self, nums, target):
# hash for index-value mapping
hash = {}
# loop over nums, and sync it with hash
for i in range(len(nums)):
if target - nums[i] in hash:
return [hash[target - nums[i]], i]
hash[nums[i]] = i
# in case if there is no solution (two sum)
return [-1, -1]
# V2
# example :
# nums = [3,3] , target = 6 -> return [0, 1]
# nums = [0,3,3] , target = 6 -> return [1, 2]
class Solution(object):
def twoSum(self, nums, target):
for i,num in enumerate(nums):
if target - num in nums[i+1:]:
return [nums.index(num),nums[i+1:].index( target - num) +i+1 ]
else:
pass
# V3
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
lookup = {}
for i, num in enumerate(nums):
if target - num in lookup:
return [lookup[target - num], i]
lookup[num] = i
def twoSum2(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in nums:
j = target - i
tmp_nums_start_index = nums.index(i) + 1
tmp_nums = nums[tmp_nums_start_index:]
if j in tmp_nums:
return [nums.index(i), tmp_nums_start_index + tmp_nums.index(j)]