From 3d812ba870c51e3ab7fcfaa52fae9d12115f41c7 Mon Sep 17 00:00:00 2001 From: Tomi Jaga Date: Sat, 16 Jul 2022 00:41:05 -0400 Subject: [PATCH] Rust: Two Pointers (two_sum, 3sum)) --- .../217_contains_duplicates.rs | 8 ++-- .../347_top_k_frequent_elements.rs | 2 +- rust/01_arrays_&_hashing/49_group_anagrams.rs | 6 +-- .../11_container_with_most_water.rs | 2 +- rust/02_two_pointers/15_3sum.rs | 44 +++++++++++++++++++ rust/02_two_pointers/167_two_sum_II.rs | 19 ++++++++ 6 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 rust/02_two_pointers/15_3sum.rs create mode 100644 rust/02_two_pointers/167_two_sum_II.rs diff --git a/rust/01_arrays_&_hashing/217_contains_duplicates.rs b/rust/01_arrays_&_hashing/217_contains_duplicates.rs index bd67fba12..985d8793d 100644 --- a/rust/01_arrays_&_hashing/217_contains_duplicates.rs +++ b/rust/01_arrays_&_hashing/217_contains_duplicates.rs @@ -4,15 +4,15 @@ impl Solution { pub fn contains_duplicate(nums: Vec) -> 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 } -} \ No newline at end of file +} diff --git a/rust/01_arrays_&_hashing/347_top_k_frequent_elements.rs b/rust/01_arrays_&_hashing/347_top_k_frequent_elements.rs index bff61e442..76a55e8b7 100644 --- a/rust/01_arrays_&_hashing/347_top_k_frequent_elements.rs +++ b/rust/01_arrays_&_hashing/347_top_k_frequent_elements.rs @@ -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) diff --git a/rust/01_arrays_&_hashing/49_group_anagrams.rs b/rust/01_arrays_&_hashing/49_group_anagrams.rs index 6cf02a58b..526399565 100644 --- a/rust/01_arrays_&_hashing/49_group_anagrams.rs +++ b/rust/01_arrays_&_hashing/49_group_anagrams.rs @@ -2,10 +2,10 @@ use std::collections::HashMap; impl Solution { pub fn group_anagrams(strs: Vec) -> Vec> { - let mut map: HashMap<[u16; 26], Vec> = HashMap::new(); + let mut map: HashMap<[u8; 26], Vec> = 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; @@ -18,6 +18,6 @@ impl Solution { } } - map.into_values().collect::>>() + map.into_values().collect() } } \ No newline at end of file diff --git a/rust/02_two_pointers/11_container_with_most_water.rs b/rust/02_two_pointers/11_container_with_most_water.rs index 2cbd66c07..e52c8e0b1 100644 --- a/rust/02_two_pointers/11_container_with_most_water.rs +++ b/rust/02_two_pointers/11_container_with_most_water.rs @@ -15,7 +15,7 @@ impl Solution { max = area; } - if rh< lh{ + if rh < lh{ while r > 0 && height[r] <= rh{ r-=1; } diff --git a/rust/02_two_pointers/15_3sum.rs b/rust/02_two_pointers/15_3sum.rs new file mode 100644 index 000000000..f66ec600e --- /dev/null +++ b/rust/02_two_pointers/15_3sum.rs @@ -0,0 +1,44 @@ +impl Solution { + pub fn three_sum(mut nums: Vec) -> Vec> { + 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 + } +} \ No newline at end of file diff --git a/rust/02_two_pointers/167_two_sum_II.rs b/rust/02_two_pointers/167_two_sum_II.rs new file mode 100644 index 000000000..00095e04d --- /dev/null +++ b/rust/02_two_pointers/167_two_sum_II.rs @@ -0,0 +1,19 @@ +impl Solution { + pub fn two_sum(numbers: Vec, target: i32) -> Vec { + 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!() + } +} \ No newline at end of file