-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum.py
55 lines (38 loc) · 2.19 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Link: https://leetcode.com/problems/3sum/submissions/
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
# Sort list
nums.sort()
# Create a new array for returnal
ans = list()
# Iterate nums with a buffer size of 3 (prevents out-of-bound error)
for i in range(len(nums) - 2):
# Skip if number is 0 AND matches previous element
if i > 0 and nums[i] == nums[i-1]:
continue
# Create pointers at both endpoints
leftPointer = i + 1
rightPointer = len(nums) - 1
# Iterate sublist nums[leftPointer:rightPointer]
while (leftPointer < rightPointer):
# Compute sum of both pointers and current element
sum = nums[i] + nums[leftPointer] + nums[rightPointer]
# NOTE: Remember that the list is in sorted order!
# Update leftPointer by going to the next high number
if sum < 0:
leftPointer += 1
# Update rightPointer by going to the next low number
elif sum > 0:
rightPointer -= 1
else:
# If sum == 0, then add combination to ans[]
ans.append([nums[i], nums[leftPointer], nums[rightPointer]])
# Update leftPointer while it hasn't reached out of bounds AND it doesn't match with last element
while leftPointer < len(nums) - 1 and nums[leftPointer] == nums[leftPointer + 1]:
leftPointer += 1
# Update rightPointer while it hasn't reached out of bounds AND it doesn't match with last element
while rightPointer > 0 and nums[rightPointer] == nums[rightPointer - 1]:
rightPointer -= 1
leftPointer += 1
rightPointer -= 1
return ans