-
Notifications
You must be signed in to change notification settings - Fork 0
/
014 3Sum.py
executable file
·129 lines (110 loc) · 3.66 KB
/
014 3Sum.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the
array which gives the sum of zero.
Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a <= b <= c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
"""
__author__ = 'Danyang'
class Solution:
def threeSum_duplicate(self, num):
"""
Hash
O(n^2)
:param num: array
:return: a list of lists of length 3, [[val1,val2,val3]]
"""
reverse_map = {}
for ind, val in enumerate(num):
if val not in reverse_map:
reverse_map[val] = [ind]
else:
reverse_map[val].append(ind)
result = []
for i in xrange(len(num)):
for j in xrange(i, len(num)):
target = 0-num[i]-num[j]
if target not in reverse_map:
continue
for index in reverse_map[target]:
if i != index and j != index:
result.append([num[i], num[j], target])
break
return result
def threeSum_TLE(self, num):
"""
Hash
O(n^2)
:param num: array
:return: a list of lists of length 3, [[val1,val2,val3]]
"""
# hash
reverse_map = {}
for ind, val in enumerate(num):
if val not in reverse_map:
reverse_map[val] = [ind]
else:
reverse_map[val].append(ind)
result = {}
for i in xrange(len(num)):
for j in xrange(i, len(num)):
target = 0-num[i]-num[j]
if target not in reverse_map:
continue
for index in reverse_map[target]:
if index != i and index != j:
lst = sorted([num[i], num[j], target])
lst = tuple(lst)
result[lst] = 1 # hash
break
return result.keys()
def threeSum(self, num):
"""
Brute force first, then determine whether sorting time complexity exceeds the brute force
Three pointers
Algorithm:
1. sort everything first
2. three pointers, i, j, k
3. notice JUMP
Palantir Technology Interview Question
http://en.wikipedia.org/wiki/3SUM
O(n^2)
Notice:
0. duplicated element will cause "Output Limit Exceeded" error
1. avoid using set
:param num: array
:return: a list of lists of length 3, [[val1,val2,val3]]
"""
# record result
result = []
num.sort() # sorting first, avoid duplicate,
i = 0
while i < len(num)-2:
j = i+1
k = len(num)-1
while j < k:
lst = [num[i], num[j], num[k]]
if sum(lst) == 0:
result.append(lst)
k -= 1
j += 1
# JUMP remove duplicate
while j < k and num[j] == num[j-1]:
j += 1
while j < k and num[k] == num[k+1]:
k -= 1
elif sum(lst) > 0:
k -= 1
else:
j += 1
i += 1
# remove duplicate
while i < len(num)-2 and num[i] == num[i-1]:
i += 1
return result
if __name__ == "__main__":
print Solution().threeSum([-1, 0, 1, 2, -1, -4])