-
Notifications
You must be signed in to change notification settings - Fork 43
/
two-sum-ii-input-array-is-sorted.py
183 lines (160 loc) · 5.33 KB
/
two-sum-ii-input-array-is-sorted.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
167. Two Sum II - Input array is sorted
Easy
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= first < second <= numbers.length.
Return the indices of the two numbers, index1 and index2, as an integer array [index1, index2] of length 2.
The tests are generated such that there is exactly one solution. You may not use the same element twice.
Example 1:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
Example 2:
Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3.
Example 3:
Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2.
Constraints:
2 <= numbers.length <= 3 * 104
-1000 <= numbers[i] <= 1000
numbers is sorted in non-decreasing order.
-1000 <= target <= 1000
The tests are generated such that there is exactly one solution.
"""
# V0
# IDEA : TWO POINTERS
# -> l = 0, r = len(numbers) - 1
class Solution(object):
def twoSum(self, numbers, target):
l = 0
### NOTE this
r = len(numbers) - 1
res = []
while r > l:
#print ("l, r = {}, {}".format(l, r))
tmp = numbers[l] + numbers[r]
if tmp == target:
return [l+1, r+1]
### NOTE this
elif tmp > target:
r -= 1
### NOTE this
else:
l += 1
return [-1, -1]
# V0'
# IDEA : TWO POINTERS
class Solution(object):
def twoSum(self, numbers, target):
left, right = 0, len(numbers) - 1
while left < right:
if numbers[left] + numbers[right] == target:
return [left + 1, right + 1]
elif numbers[left] + numbers[right] > target:
right -= 1
else:
left += 1
# V0'
# IDEA : DICT
class Solution(object):
def twoSum(self, numbers, target):
num_dict = {}
for i, num in enumerate(numbers):
if (target - num) in num_dict:
return [num_dict[target - num], i + 1]
num_dict[num] = i + 1
# V0''
# IDEA : BINARY SEARCH
class Solution(object):
def twoSum(self, numbers, target):
### NOTE : we still need loop on numbers
for i in range(len(numbers)):
l, r = i+1, len(numbers)-1
### NOTE : we still need tmp = target - numbers[i]
tmp = target - numbers[i]
while l <= r:
mid = l + (r-l)//2
if numbers[mid] == tmp:
return [i+1, mid+1]
elif numbers[mid] < tmp:
### binary search pattern
l = mid+1
else:
### binary search pattern
r = mid-1
# V1
# https://blog.csdn.net/coder_orz/article/details/52388066
# IDEA : TWO POINTER
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
left, right = 0, len(numbers) - 1
while left < right:
if numbers[left] + numbers[right] == target:
return [left + 1, right + 1]
elif numbers[left] + numbers[right] > target:
right -= 1
else:
left += 1
### Test case
s=Solution()
assert s.twoSum([1,2,3,4],5) == [1,4]
assert s.twoSum([1,2,3,4],6) == [2,4]
assert s.twoSum([1,2,3,4],7) == [3,4]
assert s.twoSum([1,2],3) == [1,2]
assert s.twoSum([1,1,1,1],2) == [1,4]
assert s.twoSum([-1,1,2,3],0) == [1,2]
assert s.twoSum([-1,1,2,3],2) == [1,4]
assert s.twoSum([],2) == None
assert s.twoSum([],0) == None
# V1'
# https://blog.csdn.net/coder_orz/article/details/52388066
# IDEA : DICT
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
num_dict = {}
for i, num in enumerate(numbers):
if (target - num) in num_dict:
return [num_dict[target - num], i + 1]
num_dict[num] = i + 1
# V1''
# https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/51249/Python-different-solutions-(two-pointer-dictionary-binary-search).
# IDEA : BINARY SEARCH
class Solution(object):
def twoSum(self, numbers, target):
for i in range(len(numbers)):
l, r = i+1, len(numbers)-1
tmp = target - numbers[i]
while l <= r:
mid = l + (r-l)//2
if numbers[mid] == tmp:
return [i+1, mid+1]
elif numbers[mid] < tmp:
l = mid+1
else:
r = mid-1
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def twoSum(self, nums, target):
start, end = 0, len(nums) - 1
while start != end:
sum = nums[start] + nums[end]
if sum > target:
end -= 1
elif sum < target:
start += 1
else:
return [start + 1, end + 1]