Skip to content

Commit 09faf30

Browse files
Refactor : 3sum
1 parent 84b3725 commit 09faf30

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

3sum/printjin-gmailcom.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
class Solution:
22
def threeSum(self, nums):
3-
triplets = set()
3+
triplets = []
44
nums.sort()
55
for i in range(len(nums) - 2):
6+
if i > 0 and nums[i - 1] == nums[i]:
7+
continue
68
low, high = i + 1, len(nums) - 1
79
while low < high:
810
three_sum = nums[i] + nums[low] + nums[high]
@@ -11,6 +13,10 @@ def threeSum(self, nums):
1113
elif three_sum > 0:
1214
high -= 1
1315
else:
14-
triplets.add((nums[i], nums[low], nums[high]))
16+
triplets.append([nums[i], nums[low], nums[high]])
17+
while low < high and nums[low] == nums[low + 1]:
18+
low += 1
19+
while low < high and nums[high] == nums[high - 1]:
20+
high -= 1
1521
low, high = low + 1, high - 1
16-
return list(triplets)
22+
return triplets

0 commit comments

Comments
 (0)