-
Notifications
You must be signed in to change notification settings - Fork 2
/
3Sum.py
34 lines (32 loc) · 1020 Bytes
/
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
class Solution(object):
def threeSum(self, numz):
db = {}
db2 = {}
nums = []
for val in numz:
if (val not in db):
db[val] = 0
db[val] += 1
if (db[val] < 4):
nums.append(val)
def all_in(threeVals):
temp = {}
for val in threeVals:
if (val not in temp):
temp[val] = 0
temp[val] += 1
if (temp[val] > db[val]):
return False
return threeVals
a = []
b = {}
for i in xrange(len(nums)):
for e in xrange(i, len(nums)):
if ((0 - (nums[i] + nums[e])) in db):
v = [nums[i], nums[e], (0 - (nums[i] + nums[e]))]
v.sort()
if (str(v) not in b):
if (all_in(v) != False):
a.append(v)
b[str(v)] = True
return a