Skip to content

Commit 4b9aec2

Browse files
3Sum
1 parent 0f20b2b commit 4b9aec2

File tree

1 file changed

+25
-0
lines changed
  • LeetCode Solutions/LeetCoding Challenge/October 2021 LeetCoding Challenge

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
nums.sort()
4+
triplets = []
5+
length = len(nums)
6+
for i in range(length-2):
7+
if(i > 0 and nums[i] == nums[i-1]):
8+
continue
9+
left = i + 1
10+
right = length - 1
11+
while(left < right):
12+
total = nums[i] + nums[left] + nums[right]
13+
if(total < 0):
14+
left += 1
15+
elif(total > 0):
16+
right -= 1
17+
else:
18+
triplets.append([nums[i], nums[left], nums[right]])
19+
while(left < right and nums[left] == nums[left+1]):
20+
left += 1
21+
while(left < right and nums[right] == nums[right-1]):
22+
right -= 1
23+
left += 1
24+
right -= 1
25+
return triplets

0 commit comments

Comments
 (0)