-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathanswer.py
112 lines (96 loc) · 3.32 KB
/
answer.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
#!/usr/bin/python
from itertools import permutations
#------------------------------------------------------------------------------
# Brute Force Solution
class Solution(object):
def threeSuma(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
length = len(nums)
nums.sort()
for i in xrange(length - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
j = i + 1
k = length - 1
while j < k:
s = nums[i] + nums[j] + nums[k]
if s < 0:
j += 1
elif s > 0:
k -= 1
else:
result.append([nums[i], nums[j], nums[k]])
while j < k and nums[j] == nums[j + 1]:
j += 1
while j < k and nums[k] == nums[k - 1]:
k -= 1
j += 1
k += 1
return result
def threeSum(self, nums):
res = []
nums.sort()
for i in xrange(len(nums)-2):
if i > 0 and nums[i] == nums[i-1]:
continue
l, r = i+1, len(nums)-1
while l < r:
s = nums[i] + nums[l] + nums[r]
if s < 0:
l +=1
elif s > 0:
r -= 1
else:
res.append((nums[i], nums[l], nums[r]))
while l < r and nums[l] == nums[l+1]:
l += 1
while l < r and nums[r] == nums[r-1]:
r -= 1
l += 1; r -= 1
return res
def threeSumBrute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
"""
for i in nums:
for j in nums:
if j == i:
continue
for k in nums:
if k == j or k == i:
continue
if (i + j + k) == 0:
result.append([i, j, k])
"""
length = len(nums)
for i in range(length):
for j in range(length):
if j <= i:
continue
for k in range (length):
if k <= j or k <= i:
continue
if (nums[i] + nums[j] + nums[k]) == 0:
sum = ([nums[i], nums[j], nums[k]])
# Check if there is a permutation inside already
inside = False
for per in list(permutations(sum, 3)):
if per in result:
print "hello"
inside = True
break
if not inside:
result.append(sum)
return result
#------------------------------------------------------------------------------
#Testing
def main():
print Solution().threeSum([-1, 0, 1, 2, -1, -4])
main()