Skip to content

Create 3Sum.py #11656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions data_structures/arrays/3Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def three_sum(nums):

Check failure on line 1 in data_structures/arrays/3Sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

data_structures/arrays/3Sum.py:1:1: N999 Invalid module name: '3Sum'
# Sort the array to simplify the problem
nums.sort()
result = []

Check failure on line 5 in data_structures/arrays/3Sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/3Sum.py:5:1: W293 Blank line contains whitespace
# Iterate over the array
for i in range(len(nums)):
# Skip duplicates for the first element
if i > 0 and nums[i] == nums[i - 1]:
continue

Check failure on line 11 in data_structures/arrays/3Sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/3Sum.py:11:1: W293 Blank line contains whitespace
# Two pointers approach
left, right = i + 1, len(nums) - 1
while left < right:
total = nums[i] + nums[left] + nums[right]

Check failure on line 16 in data_structures/arrays/3Sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/3Sum.py:16:1: W293 Blank line contains whitespace
if total == 0:
# Found a valid triplet
result.append([nums[i], nums[left], nums[right]])
left += 1
right -= 1

Check failure on line 22 in data_structures/arrays/3Sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/3Sum.py:22:1: W293 Blank line contains whitespace
# Skip duplicates for the second and third elements
while left < right and nums[left] == nums[left - 1]:
left += 1
while left < right and nums[right] == nums[right + 1]:
right -= 1

Check failure on line 28 in data_structures/arrays/3Sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/3Sum.py:28:1: W293 Blank line contains whitespace
elif total < 0:
# Move the left pointer to increase the sum
left += 1
else:
# Move the right pointer to decrease the sum
right -= 1

Check failure on line 35 in data_structures/arrays/3Sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/3Sum.py:35:1: W293 Blank line contains whitespace
return result
Loading