forked from alok1994/pythonExample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsumTwoNumbers
33 lines (30 loc) · 939 Bytes
/
sumTwoNumbers
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
class Solution:
def sumTwo(self, arr, target):
if len(arr) < 2:
return
res = set()
arr.sort()
for i in range(len(arr)):
left = i
right = len(arr) - 1
while left < right:
total = arr[left] + arr[right]
if total < target:
left += 1
elif total > target:
right -= 1
else:
res.add((arr[left], arr[right]))
while left < right and arr[left] == arr[left + 1]:
left += 1
while left < right and arr[right] == arr[right -1]:
right -= 1
left += 1
right -= 1
return res
arr = [-1,3,0,2,1,7,8,9,6]
target = 9
s = Solution()
print(arr)
print(s.sumTwo(arr,target))
# output: {(1, 8), (0, 9), (2, 7), (3, 6)}