-
Notifications
You must be signed in to change notification settings - Fork 0
/
15. 3Sum.py
31 lines (26 loc) · 1014 Bytes
/
15. 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
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
solution_set = []
le = len(nums)
nums.sort()
if le < 3:
return solution_set
else:
for i in range(le-1):
if i != 0:
if nums[i] == nums[i-1]:
continue
j = i + 1
k = le - 1
while j < k:
sums = nums[i] + nums[j] + nums[k]
if sums == 0:
solution_set.append([nums[i], nums[j], nums[k]])
jval, kval = nums[j], nums[k]
while j < k and nums[j] == jval: j += 1
while j < k and nums[k] == kval: k -= 1
elif sums > 0:
k -= 1
else:
j += 1
return solution_set