-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_sum.py
107 lines (81 loc) · 3.06 KB
/
3_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
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
三数之和
**思路:**这是知乎职人介绍所上,两位大牛手写的算法题。我的思路是先排序,然后两个 for 循环加二分查找,时间复杂度为O(pow(N, 2)*logN),但还是没通过,有时间限制。后来结合 2Sum,想到不用两个 for 循环,直接用一个 for 循环,加二分查找。
第一种方法:
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def binary_search_right(nums, target):
if not nums:
return -1
low = 0
high = len(nums)-1
res = -1
while low <= high:
middle = (low + high)/2
if nums[middle] == target:
res = middle
if nums[middle] > target:
high = middle - 1
else:
low = middle + 1
return res
nums.sort()
res = []
for i in range(len(nums) - 1):
if (i > 0) and (nums[i] == nums[i - 1]):
continue
for j in range(i + 1, len(nums)):
if (j > i + 1) and (nums[j] == nums[j - 1]):
continue
x, y = nums[i], nums[j]
target = 0 - x - y
index = binary_search_right(nums[j+1:], target)
if (index != -1):
res.append([x, y, 0 - x - y])
return res
第二种方法:
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) <= 2:
return []
def two_sum(nums, target):
low = 0
high = len(nums) - 1
res = []
while low < high:
if (low > 0) and (nums[low] == nums[low-1]):
low += 1
continue
if (high<len(nums)-1) and (nums[high]==nums[high+1]):
high -= 1
continue
if low >= high:
return res
sum = nums[low] + nums[high]
if sum == target:
res.append([nums[low], nums[high]])
low += 1
high -= 1
elif sum < target:
low += 1
else:
high -= 1
return res
nums.sort()
res = []
for i in range(len(nums) - 1):
if (i > 0) and (nums[i] == nums[i - 1]):
continue
temp = two_sum(nums[i+1:], -nums[i])
if temp:
for t in temp:
t.append(nums[i])
res.append(t)
return res