-
Notifications
You must be signed in to change notification settings - Fork 104
Added Rust solutions for Chapter 1: Two Pointers #86
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
Destiny-02
merged 6 commits into
ByteByteGoHq:main
from
gitsudhir:rust-solutions-two-pointers
Jun 5, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f81939
two pointers > pair sum sorted
gitsudhir 902cb59
two pointers > triplet sum
gitsudhir d077819
two pointers > is palindrome valid
gitsudhir 8cb6812
two pointers > largest container
gitsudhir 64f92a1
Refactor: Align Rust solution with Python logic and naming conventions
gitsudhir bce4171
Remove .to_lowercase() conversion as suggested
gitsudhir 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
fn is_palindrome_valid(s: &str) -> bool { | ||
// Edge case | ||
if s.len() == 0 || s.len() == 1 { | ||
return true; | ||
} | ||
|
||
let chars: Vec<_> = s.chars().collect(); | ||
let mut first_pointer = 0; | ||
let mut last_pointer = s.len() - 1; | ||
while first_pointer < last_pointer { | ||
// Skip non-alphanumeric characters from the left. | ||
let first_char = chars[first_pointer]; | ||
Destiny-02 marked this conversation as resolved.
Show resolved
Hide resolved
Destiny-02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if !first_char.is_alphanumeric() { | ||
first_pointer += 1; | ||
continue; | ||
} | ||
|
||
// Skip non-alphanumeric characters from the right. | ||
let last_char = chars[last_pointer]; | ||
if !last_char.is_alphanumeric() { | ||
last_pointer -= 1; | ||
continue; | ||
} | ||
|
||
// If the characters at the left and right pointers don't | ||
// match, the string is not a palindrome. | ||
if first_char.to_ascii_lowercase() != last_char.to_ascii_lowercase() { | ||
Destiny-02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false; | ||
} | ||
|
||
// Shrink the pointers | ||
first_pointer += 1; | ||
last_pointer -= 1; | ||
} | ||
true | ||
} |
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,34 @@ | ||
use std::{ | ||
cmp::{max, min}, | ||
i32::MIN, | ||
}; | ||
|
||
fn largest_container(heights: Vec<i32>) -> i32 { | ||
// edge case | ||
if heights.len() == 1 { | ||
return 0; | ||
} | ||
|
||
let mut first_pointer = 0; | ||
let mut last_pointer = heights.len() - 1; | ||
let mut max_water = MIN; | ||
|
||
while first_pointer < last_pointer { | ||
// Calculate the water contained between the current pair of lines. | ||
let water = min(heights[first_pointer], heights[last_pointer]) | ||
* (last_pointer as i32 - first_pointer as i32); | ||
// Move the pointers inward, always moving the pointer at the | ||
// shorter line. If both lines have the same height, move both | ||
// pointers inward. | ||
max_water = max(max_water, water); | ||
if heights[first_pointer] < heights[last_pointer] { | ||
first_pointer += 1; | ||
} else if heights[first_pointer] > heights[last_pointer] { | ||
last_pointer -= 1; | ||
} else { | ||
first_pointer += 1; | ||
last_pointer -= 1; | ||
} | ||
} | ||
max_water | ||
} |
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,22 @@ | ||
fn pair_sum_sorted(nums: &[i32], target: i32) -> Vec<usize> { | ||
let mut first_pointer = 0; | ||
let mut last_pointer = nums.len() - 1; | ||
while first_pointer < last_pointer { | ||
let first_value = nums[first_pointer]; | ||
let last_value = nums[last_pointer]; | ||
let cur_sum = first_value + last_value; | ||
// If the sum is smaller, increment the left pointer, aiming | ||
// to increase the sum toward the target value. | ||
if cur_sum < target { | ||
first_pointer += 1; | ||
} else if cur_sum > target { | ||
// If the sum is larger, decrement the right pointer, aiming | ||
// to decrease the sum toward the target value. | ||
last_pointer -= 1; | ||
} else { | ||
// If the target pair is found, return its indexes. | ||
return vec![first_pointer, last_pointer]; | ||
} | ||
} | ||
vec![] | ||
} |
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,46 @@ | ||
fn triplet_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> { | ||
// Sort the vector | ||
nums.sort(); | ||
// a + b + c = 0 | ||
// use the pair sum sorted , b + c = -a (target); | ||
let len = nums.len(); | ||
let mut result: Vec<Vec<i32>> = vec![]; | ||
|
||
for index in 0..len - 1 { | ||
// To avoid duplicate triplets, skip 'a' if it's the same as | ||
// the previous number. | ||
if index > 0 && nums[index] == nums[index - 1] { | ||
continue; | ||
} | ||
// Find all pairs that sum to a target of '-a' (-nums[i]). | ||
let triplet = pair_sum_sorted(&nums[index..], -nums[index]); | ||
if triplet.len() != 0 { | ||
result.push(triplet); | ||
} | ||
} | ||
|
||
result | ||
} | ||
|
||
fn pair_sum_sorted(remain_nums: &[i32], target: i32) -> Vec<i32> { | ||
let mut first_pointer = 0; | ||
let mut last_pointer = remain_nums.len() - 1; | ||
|
||
while first_pointer < last_pointer { | ||
let sum = remain_nums[first_pointer] + remain_nums[last_pointer]; | ||
// If sum is equal to target return the triplet [a,b,c] | ||
if sum == target { | ||
return vec![ | ||
-target, | ||
remain_nums[first_pointer], | ||
remain_nums[last_pointer], | ||
]; | ||
} else if sum < target { | ||
first_pointer += 1; | ||
} else { | ||
last_pointer -= 1; | ||
} | ||
} | ||
// If there is no triplet return empty vector | ||
vec![] | ||
} |
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.