-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeSum.py
More file actions
49 lines (37 loc) · 1.19 KB
/
threeSum.py
File metadata and controls
49 lines (37 loc) · 1.19 KB
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
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
tempNums = nums
result = []
tempNums.sort()
if len(nums) < 3:
return [nums]
if len(nums) == 3:
if (tempNums[0] + tempNums[1] + tempNums[2]) == 0:
return [tempNums]
else:
return []
for i in range(len(tempNums)):
if i > 0 and tempNums[i] == tempNums[i - 1]:
continue
l, r = i+1, len(tempNums) - 1
while l < r:
currSum = tempNums[l] + tempNums[i] + tempNums[r]
if currSum == 0:
result.append(
[tempNums[i], tempNums[l], tempNums[r]]
)
l = l + 1
while l < r and (nums[l] == nums[l-1]):
l += 1
elif currSum < 0:
l += 1
elif currSum > 0:
r -= 1
return result
temp = Solution()
# print(temp.threeSum([-1,-3,-4, 4, 0, 1]))
print(temp.threeSum([-1,0,1,2,-1,-4]))