-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.py
65 lines (50 loc) · 1.85 KB
/
15.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums = sorted(nums)
d_nums = []
num_count = dict()
for num in nums:
if num in num_count:
num_count[num] += 1
if num_count[num] <= 2:
d_nums.append(num)
else:
num_count[num] = 1
d_nums.append(num)
array_set = dict()
arrays = []
for i in xrange(0, len(d_nums)):
if d_nums[i] > 0:
break
for j in xrange(i+1, len(d_nums)):
if d_nums[i] + d_nums[j] > 0:
break
for k in xrange(j+1, len(d_nums)):
if d_nums[i] + d_nums[j] + d_nums[k] > 0:
break
if d_nums[i] + d_nums[j] + d_nums[k] == 0:
if d_nums[i] in array_set and d_nums[j] in array_set[d_nums[i]]:
pass
else:
arrays.append([d_nums[i], d_nums[j], d_nums[k]])
if d_nums[i] not in array_set:
array_set[d_nums[i]] = set([d_nums[j]])
else:
if d_nums[j] not in array_set[d_nums[i]]:
array_set[d_nums[i]].add(d_nums[j])
break
if 0 in num_count and num_count[0] >= 3:
if 0 in array_set and 0 in array_set[0]:
pass
else:
arrays.append([0, 0, 0])
return arrays
if __name__ == "__main__":
print Solution().threeSum([])