-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree_sum.py
36 lines (34 loc) · 1.24 KB
/
three_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
class Solution:
def threeSum(self, nums):
result = []
print(float('inf'))
for i in range(0, len(nums)):
for j in range(i+1, len(nums)):
for k in range(j+1, len(nums)):
if nums[i] + nums[j] + nums[k] == 0:
childResult = [nums[i], nums[j], nums[k]]
childResult = sorted(childResult)
if (childResult not in result):
result.append(childResult)
return result
def threeSum2(self, nums):
nums = sorted(nums)
ans = []
target = float('inf')
for i in range(len(nums)):
if -nums[i] == target:
continue
target = -nums[i]
l, r = i + 1, len(nums) - 1
fl, fr = float('inf'), float('inf')
while l < r:
if nums[l] + nums[r] == target:
if fl != nums[l] or fr != nums[r]:
ans.append([-target, nums[l], nums[r]])
fl, fr = nums[l], nums[r]
r -= 1
elif nums[l] + nums[r] < target:
l += 1
else:
r -= 1
return ans