-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.py
More file actions
48 lines (39 loc) · 1.1 KB
/
15.py
File metadata and controls
48 lines (39 loc) · 1.1 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
import sys
def main():
line = sys.stdin.read().strip().split()
nums = list(map(int,line))
sol = Solution()
result = sol.threeSum(nums)
print(result)
class Solution:
def threeSum(self, nums):
nums.sort()
ans = []
n = len(nums)
for i in range(n-2):
x = nums[i]
if i > 0 and x == nums[i-1]:
continue
if x + nums[i+1] + nums[i+2] > 0:
break
if x + nums[-1] + nums[-2] < 0:
continue
j = i + 1
k = n - 1
while j<k:
sum = x + nums[j] + nums[k]
if sum == 0:
ans.append([x, nums[j], nums[k]])
j+=1
while j<k and nums[j] == nums[j-1]:
j+=1
k-=1
while j<k and nums[k] == nums[k+1]:
k-=1
elif sum < 0:
j+=1
elif sum > 0:
k-=1
return ans
if __name__ == "__main__":
main()