-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.py
More file actions
43 lines (34 loc) · 922 Bytes
/
Copy path1.py
File metadata and controls
43 lines (34 loc) · 922 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
40
41
42
43
def twoSum(nums,target):
mylist=[]
positions=[]
for i in range(len(nums)):
positions.append(i)
combined = list(zip(nums, positions))
sorted_combined = sorted(combined, key=lambda x: x[0])
nums,positions = zip(*sorted_combined)
#print(nums," ",positions)
l=0
r=len(nums)-1
while l<r:
if (nums[l]+nums[r])<target:
l+=1
elif (nums[l]+nums[r])>target:
r-=1
else:
mylist.append([positions[l],positions[r]])
l+=1
r-=1
return mylist
nums = [3,2,4]
target = 6
result=twoSum(nums,target)
#result=twoSum([-3,3,2,4,9],6)
print(result)
# BRUTE FORCE:
"""
def twoSum(self, nums, target):
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i]+nums[j]==target:
return i,j
"""