Skip to content

Commit

Permalink
Rust: Two Pointers (two_sum, 3sum))
Browse files Browse the repository at this point in the history
  • Loading branch information
tomijaga committed Jul 16, 2022
1 parent 3a8d6d4 commit 3d812ba
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 9 deletions.
8 changes: 4 additions & 4 deletions rust/01_arrays_&_hashing/217_contains_duplicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ impl Solution {
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
let mut map = HashSet::new();

for n in nums.iter(){
for &n in nums.iter(){

if map.contains(n){
if map.contains(&n){
return true;
}

map.insert(n);
};

false
false
}
}
}
2 changes: 1 addition & 1 deletion rust/01_arrays_&_hashing/347_top_k_frequent_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Solution {

let mut freq: Vec<(i32, i32)> = map.into_iter().collect();

let res = if k == arr.len() as i32{
let res = if k == freq.len() as i32{
&freq
}else{
quick_select(&mut freq, k)
Expand Down
6 changes: 3 additions & 3 deletions rust/01_arrays_&_hashing/49_group_anagrams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::collections::HashMap;

impl Solution {
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
let mut map: HashMap<[u16; 26], Vec<String>> = HashMap::new();
let mut map: HashMap<[u8; 26], Vec<String>> = HashMap::new();

for s in strs{
let mut key = [0_u16; 26];
let mut key = [0_u8; 26];

for c in s.chars(){
key[c as usize - 'a' as usize] += 1;
Expand All @@ -18,6 +18,6 @@ impl Solution {
}
}

map.into_values().collect::<Vec<Vec<String>>>()
map.into_values().collect()
}
}
2 changes: 1 addition & 1 deletion rust/02_two_pointers/11_container_with_most_water.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Solution {
max = area;
}

if rh< lh{
if rh < lh{
while r > 0 && height[r] <= rh{
r-=1;
}
Expand Down
44 changes: 44 additions & 0 deletions rust/02_two_pointers/15_3sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
impl Solution {
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
nums.sort_unstable();

let mut res = vec![];

for i in 0..nums.len() - 1{
let n1 = nums[i];

if i > 0 && n1 == nums[i - 1]{
continue;
}

let (mut l, mut r) = (i + 1, nums.len() - 1);

while l < r{
let n2 = nums[l];
let n3 = nums[r];

let sum = n1 + n2 + n3;

if sum > 0{
r-=1;
}else if sum < 0{
l+=1;
}else{
res.push(vec![n1, n2, n3]);

while l < r && n2 == nums[l + 1]{
l+=1;
}
l+=1;

while l < r && n3 == nums[r - 1]{
r-=1;
}
r-=1;
}
}
}

res
}
}
19 changes: 19 additions & 0 deletions rust/02_two_pointers/167_two_sum_II.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
impl Solution {
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
let (mut l, mut r) = (0, numbers.len() - 1);

while l < r{
let sum = numbers[l] + numbers[r];

if sum > target{
r-=1;
}else if sum < target{
l+=1;
}else{
return vec![(l + 1) as i32, (r + 1) as i32];
}
}

unreachable!()
}
}

0 comments on commit 3d812ba

Please sign in to comment.