-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum_leetcode.py
58 lines (49 loc) · 1.56 KB
/
3Sum_leetcode.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
# nums=[-1,0,1,2,-1,-4] # method 1
# res=[]
# store_dict={}
# l=len(nums)
# nums.sort()
# for ele in nums:
# if ele not in store_dict:
# store_dict[ele]=1
# else:
# store_dict[ele]=store_dict[ele]+1
# print(store_dict)
# for i in range(l):
# t2=store_dict[nums[i]] # need to modify some code here
# store_dict.pop(nums[i])
# for j in range(i+1,l):
# t1=store_dict[nums[j]]
# store_dict.pop(nums[j])
# k=nums[i]+nums[j]
# if -k in store_dict:
# res.append([nums[i],nums[j],-k])
# store_dict[nums[j]]=t1
# store_dict[nums[i]]=t2
# print(res)
# for ele in res:
# ele.sort()
# print(res)
# ans=[]
# for ele in res:
# if ele not in ans:
# ans.append(ele)
# print(ans)
nums.sort() # method 2 --> optimized --> work for all 2,3,4 Sum
N, result = len(nums), []
for i in range(N):
if i > 0 and nums[i] == nums[i-1]:
continue
target = nums[i]*-1
s,e = i+1, N-1
while s<e:
if nums[s]+nums[e] == target:
result.append([nums[i], nums[s], nums[e]])
s = s+1
while s<e and nums[s] == nums[s-1]:
s = s+1
elif nums[s] + nums[e] < target:
s = s+1
else:
e = e-1
return result