-
Notifications
You must be signed in to change notification settings - Fork 0
Leetcode three sum #17
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
Open
Igneaalis
wants to merge
9
commits into
master
Choose a base branch
from
LEETCODE-three-sum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
20e3c76
Added solution
Igneaalis a543d98
Merge branch 'LEETCODE-subarray-sum-equals-k' of https://github.com/I…
Igneaalis d55c803
three sum doesn't work
Igneaalis 10b1363
Three sum generator solution (O^3)
Igneaalis 1d9e85d
Three sum Set + Hash solution (O^3)
Igneaalis ec1091c
Subarray sum equals k solution
Igneaalis 5eb31e4
Changed variables' name, added hyperlinks (anchors), improved [Subarr…
Igneaalis 904491f
Added LEETCODE "three-sum" solution.
Igneaalis 4a986a4
Changed a return value for a hash version.
Igneaalis 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,80 @@ | ||
| # Arrays | ||
|
|
||
| + [Three sum (Generator version, O(n^3))](#three-sum-generator-version-on3) | ||
| + [Three sum (Set + hash version O(n^2))](#three-sum-set--hash-version-on2) | ||
|
|
||
| ## Three sum (Generator version, O(n^3)) | ||
|
|
||
| https://leetcode.com/problems/3sum/ | ||
|
|
||
| ```python | ||
| class Solution(object): | ||
|
|
||
| def twoSum(self, nums, target, removed): | ||
| """ | ||
| :type nums: List[int] | ||
| :type target: int | ||
| :type removed: int | ||
| :rtype: List[int] | ||
| """ | ||
|
|
||
| for index, value in enumerate(nums): | ||
| if index == removed: | ||
| continue | ||
| if target - value in nums and index != nums.index(target - value) and removed != nums.index(target - value): | ||
| yield sorted([value, target - value]) | ||
|
|
||
| def threeSum(self, nums): | ||
| """ | ||
| :type nums: List[int] | ||
| :rtype: List[List[int]] | ||
| """ | ||
|
|
||
| triplets = [] | ||
| for index, value in enumerate(nums): | ||
| pairs = [] | ||
| for x in self.twoSum(nums, -value, index): | ||
| if x not in pairs: | ||
| pairs.append(x) | ||
| for p in pairs: | ||
| if sorted(p + [value]) not in triplets: | ||
| triplets += [sorted(p + [value])] | ||
| return triplets | ||
|
|
||
| ``` | ||
|
|
||
| ## Three sum (Set + hash version O(n^2)) | ||
|
|
||
| https://leetcode.com/problems/3sum/ | ||
|
|
||
| ```python | ||
| class Solution(object): | ||
|
|
||
| def twoSum(self, nums, target, removed): | ||
| """ | ||
| :type nums: List[int] | ||
| :type target: int | ||
| :type removed: set | ||
| :rtype: List[int] | ||
| """ | ||
|
|
||
| dict = {} | ||
| for index, value in enumerate(nums): | ||
| if target - value in dict: | ||
| removed.add((value, target - value, -target)) | ||
| dict[value] = index | ||
| del dict | ||
|
|
||
| def threeSum(self, nums): | ||
| """ | ||
| :type nums: List[int] | ||
| :rtype: List[List[int]] | ||
| """ | ||
|
|
||
| nums.sort() | ||
| out = set() | ||
| for index, value in enumerate(nums): | ||
| self.twoSum(nums[index + 1:], -value, out) | ||
| return [list(x) for x in out] | ||
|
|
||
| ``` | ||
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.
Зачем делать копии списков?
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.
Про какие копии здесь идёт речь? если про return, то я изменил запись.