-
Notifications
You must be signed in to change notification settings - Fork 2
feat(algorithms): two pointers #145
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
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c95ac53
docs(algorithms, two-pointers): three sum documentation
BrianLusina 6b00d0e
feat(algorithms, two-pointers): triangle numbers
BrianLusina b5bf32f
updating DIRECTORY.md
8ccae81
refactor(algorithms, two-pointers, move-zeroes): move algorithm to co…
BrianLusina 5c8e273
updating DIRECTORY.md
b1d3baf
chore(docstring): fix typo
BrianLusina 6aada08
refactor(algorithms, two-pointers, sort-colors): add documentation an…
BrianLusina cf07d94
refactor(algorithms, two-pointers): trapped rain water
BrianLusina 3efcce4
updating DIRECTORY.md
e899c0b
test(algorithms, two-pointers, move_zeroes): update image reference
BrianLusina 6df7b66
test(algorithms, two-pointers, move_zeroes): update type hint in test
BrianLusina 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 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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,44 @@ | ||
| # Move Zeroes | ||
|
|
||
| Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero | ||
| elements. | ||
|
|
||
| Note that you must do this in-place without making a copy of the array. | ||
|
|
||
| ```plain | ||
|
|
||
| Example 1: | ||
|
|
||
| Input: nums = [0,1,0,3,12] | ||
| Output: [1,3,12,0,0] | ||
| Example 2: | ||
|
|
||
| Input: nums = [0] | ||
| Output: [0] | ||
| ``` | ||
|
|
||
| ## Related Topics | ||
|
|
||
| - Array | ||
| - Two Pointers | ||
|
|
||
| ## Solution | ||
|
|
||
| We can solve this problem by keeping a pointer i that iterates through the array and another pointer nextNonZero that | ||
| points to the position where the next non-zero element should be placed. We can then swap the elements at i and nextNonZero | ||
| if the element at i is non-zero. This way, we can maintain the relative order of the non-zero elements while moving all | ||
| the zeroes to the end of the array. | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
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
Binary file added
BIN
+22.6 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+26.2 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+29.3 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27.4 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+22.1 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+26.5 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+29.6 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+26.8 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+26.3 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+25.9 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+29.6 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+26.2 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+26.3 KB
algorithms/two_pointers/move_zeroes/images/solutions/move_zeroes_solution_9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,31 @@ | ||
| import unittest | ||
| from typing import List | ||
| from parameterized import parameterized | ||
| from algorithms.two_pointers.move_zeroes import move_zeroes, move_zeroes_one | ||
|
|
||
| MOVE_ZEROES_TEST_CASES = [ | ||
| ([0, 1, 0, 3, 12], [1, 3, 12, 0, 0]), | ||
| ([0], [0]), | ||
| ([0, 0, 0], [0, 0, 0]), | ||
| ([1, 0], [1, 0]), | ||
| ([2, 0, 4, 0, 9], [2, 4, 9, 0, 0]), | ||
| ([1, 0, 4, 0, 3, 0, 1], [1, 4, 3, 1, 0, 0, 0]), | ||
| ([0, 0, 1], [1, 0, 0]), | ||
| ([1, 2, 3], [1, 2, 3]), | ||
| ] | ||
|
|
||
|
|
||
| class MoveZeroesTestCase(unittest.TestCase): | ||
| @parameterized.expand(MOVE_ZEROES_TEST_CASES) | ||
| def test_move_zeroes(self, nums: List[int], expected: [List]): | ||
|
BrianLusina marked this conversation as resolved.
Outdated
|
||
| move_zeroes(nums) | ||
| self.assertEqual(expected, nums) | ||
|
|
||
| @parameterized.expand(MOVE_ZEROES_TEST_CASES) | ||
| def test_move_zeroes_with_intermediate(self, nums: List[int], expected: List[int]): | ||
| move_zeroes_one(nums) | ||
| self.assertEqual(expected, nums) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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
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
Binary file added
BIN
+38.2 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+38.7 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.8 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.1 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+38.3 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.5 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+34.7 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+38.4 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+39.5 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+32.5 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+29.9 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+25.9 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24.7 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_3.png
Oops, something went wrong.
Binary file added
BIN
+32.3 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_4.png
Oops, something went wrong.
Binary file added
BIN
+35.8 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_5.png
Oops, something went wrong.
Binary file added
BIN
+35.8 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_6.png
Oops, something went wrong.
Binary file added
BIN
+40.6 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_7.png
Oops, something went wrong.
Binary file added
BIN
+36.9 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_8.png
Oops, something went wrong.
Binary file added
BIN
+37.2 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_9.png
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.