Skip to content

Commit 969a1ba

Browse files
authored
Merge pull request #86 from gitsudhir/rust-solutions-two-pointers
Added Rust solutions for Chapter 1: Two Pointers
2 parents 9002668 + bce4171 commit 969a1ba

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
fn is_palindrome_valid(s: &str) -> bool {
2+
let chars: Vec<_> = s.chars().collect();
3+
let mut left = 0;
4+
let mut right = s.len() - 1;
5+
while left < right {
6+
// Skip non-alphanumeric characters from the left.
7+
while left < right && !chars[left].is_alphanumeric(){
8+
left += 1
9+
}
10+
// Skip non-alphanumeric characters from the right.
11+
while left < right && !chars[right].is_alphanumeric(){
12+
right -= 1
13+
}
14+
// If the characters at the left and right pointers don't
15+
// match, the string is not a palindrome.
16+
if chars[left] != chars[right] {
17+
return false;
18+
}
19+
left += 1;
20+
right -= 1;
21+
}
22+
true
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::{
2+
cmp::{max, min}
3+
};
4+
5+
fn largest_container(heights: Vec<i32>) -> i32 {
6+
let mut max_water = 0;
7+
let mut left = 0;
8+
let mut right = heights.len() - 1;
9+
10+
while left < right {
11+
// Calculate the water contained between the current pair of lines.
12+
let water = min(heights[left], heights[right])
13+
* (right as i32 - left as i32);
14+
max_water = max(max_water, water);
15+
// Move the pointers inward, always moving the pointer at the
16+
// shorter line. If both lines have the same height, move both
17+
// pointers inward.
18+
if heights[left] < heights[right] {
19+
left += 1;
20+
} else if heights[left] > heights[right] {
21+
right -= 1;
22+
} else {
23+
left += 1;
24+
right -= 1;
25+
}
26+
}
27+
max_water
28+
}

rust/Two Pointers/pair_sum_sorted.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fn pair_sum_sorted(nums: &[i32], target: i32) -> Vec<usize> {
2+
let mut left = 0;
3+
let mut right = nums.len() - 1;
4+
while left < right {
5+
let sum = nums[left] + nums[right];
6+
// If the sum is smaller, increment the left pointer, aiming
7+
// to increase the sum toward the target value.
8+
if sum < target {
9+
left += 1;
10+
} else if sum > target {
11+
// If the sum is larger, decrement the right pointer, aiming
12+
// to decrease the sum toward the target value.
13+
right -= 1;
14+
} else {
15+
// If the target pair is found, return its indexes.
16+
return vec![left, right];
17+
}
18+
}
19+
vec![]
20+
}

rust/Two Pointers/triplet_sum.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
fn triplet_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
2+
let mut triplets: Vec<Vec<i32>> = vec![];
3+
nums.sort();
4+
let len = nums.len();
5+
6+
for i in 0..len {
7+
// Optimization: triplets consisting of only positive numbers
8+
// will never sum to 0.
9+
if nums[i] > 0 {
10+
break;
11+
}
12+
13+
// To avoid duplicate triplets, skip 'a' if it's the same as
14+
// the previous number.
15+
if i > 0 && nums[i] == nums[i - 1] {
16+
continue;
17+
}
18+
19+
// Find all pairs that sum to a target of '-a' (-nums[i]).
20+
let pairs = pair_sum_sorted_all_pairs(&nums, i + 1, -nums[i]);
21+
for pair in pairs {
22+
triplets.push(vec![nums[i], pair[0], pair[1]]);
23+
}
24+
}
25+
26+
triplets
27+
}
28+
29+
fn pair_sum_sorted_all_pairs(nums: &Vec<i32>, start: usize, target: i32) -> Vec<Vec<i32>> {
30+
let mut pairs = Vec::new();
31+
let mut left = start;
32+
let mut right = nums.len() - 1;
33+
34+
while left < right {
35+
let sum = nums[left] + nums[right];
36+
37+
if sum == target {
38+
pairs.push(vec![nums[left], nums[right]]);
39+
left += 1;
40+
41+
// To avoid duplicate '[b, c]' pairs, skip 'b' if it's the
42+
// same as the previous number.
43+
while left < right && nums[left] == nums[left - 1] {
44+
left += 1;
45+
}
46+
} else if sum < target {
47+
left += 1;
48+
} else {
49+
right -= 1;
50+
}
51+
}
52+
53+
pairs
54+
}

0 commit comments

Comments
 (0)