-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
add Three sum #9177
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
Merged
cclauss
merged 14 commits into
TheAlgorithms:master
from
BamaCharanChhandogi:add/three-sum
Oct 1, 2023
+47
−0
Merged
add Three sum #9177
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
636295e
add Three sum
BamaCharanChhandogi be74c5f
add Three sum
BamaCharanChhandogi 307d4cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3d688e9
update
BamaCharanChhandogi e40dff7
Merge branch 'add/three-sum' of https://github.com/BamaCharanChhandog…
BamaCharanChhandogi d5ebfd7
update
BamaCharanChhandogi 80842b8
update
BamaCharanChhandogi 6fed286
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ac4f197
update
BamaCharanChhandogi 4967bf7
Merge branch 'add/three-sum' of https://github.com/BamaCharanChhandog…
BamaCharanChhandogi 8528b19
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 730c732
add documention
BamaCharanChhandogi 16c6a46
Merge branch 'add/three-sum' of https://github.com/BamaCharanChhandog…
BamaCharanChhandogi 4fe12be
Update three_sum.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
def threeSum(nums: list[int]) -> list[list[int]]: | ||
""" | ||
Find all unique triplets in a sorted array of integers that sum up to zero. | ||
BamaCharanChhandogi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Args: | ||
nums (list[int]): A sorted list of integers. | ||
|
||
Returns: | ||
list[list[int]]: A list of lists containing unique triplets that sum up to zero. | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Example: | ||
>>> threeSum([-1, 0, 1, 2, -1, -4]) | ||
[[-1, -1, 2], [-1, 0, 1]] | ||
>>> threeSum([1, 2, 3, 4]) | ||
[] | ||
|
||
""" | ||
nums.sort() | ||
ans = [] | ||
for i in range(len(nums) - 2): | ||
if i == 0 or (nums[i] != nums[i - 1]): | ||
low, high, c = i + 1, len(nums) - 1, 0 - nums[i] | ||
while low < high: | ||
if nums[low] + nums[high] == c: | ||
ans.append([nums[i], nums[low], nums[high]]) | ||
|
||
while low < high and nums[low] == nums[low + 1]: | ||
low += 1 | ||
while low < high and nums[high] == nums[high - 1]: | ||
high -= 1 | ||
|
||
low += 1 | ||
high -= 1 | ||
elif nums[low] + nums[high] < c: | ||
low += 1 | ||
else: | ||
high -= 1 | ||
return ans | ||
|
||
# Run the doctests | ||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable and function names should follow the
snake_case
naming convention. Please update the following name accordingly:threeSum