From 65e845b2ef37ec5434b00f32e4e6d126eebf5936 Mon Sep 17 00:00:00 2001 From: phinnl Date: Thu, 9 May 2024 12:16:44 +0700 Subject: [PATCH] add four_sum --- src/count_submatrices_with_all_ones.rs | 2 + src/four_sum.rs | 71 ++++++++++++++++++++ src/letter_combinations_of_a_phone_number.rs | 2 + src/lib.rs | 3 +- 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/four_sum.rs diff --git a/src/count_submatrices_with_all_ones.rs b/src/count_submatrices_with_all_ones.rs index 38fdd50..8100dea 100644 --- a/src/count_submatrices_with_all_ones.rs +++ b/src/count_submatrices_with_all_ones.rs @@ -1,3 +1,5 @@ +// refer to https://leetcode.com/problems/count-submatrices-with-all-ones/ + pub fn num_submat(mut matrix: Vec>) -> i32 { let mut result = 0; let len = matrix.len(); diff --git a/src/four_sum.rs b/src/four_sum.rs new file mode 100644 index 0000000..f152c41 --- /dev/null +++ b/src/four_sum.rs @@ -0,0 +1,71 @@ +// refer to https://leetcode.com/problems/4sum/ + +pub fn four_sum(mut nums: Vec, target: i32) -> Vec> { + let mut result = Vec::new(); + if nums.len() < 4 { + return result; + } + + nums.sort(); + + for i in 0..nums.len() - 3 { + if i > 0 && nums[i] == nums[i - 1] { + continue; + } + for j in i + 1..nums.len() - 2 { + if j > i + 1 && nums[j] == nums[j - 1] { + continue; + } + let mut left = j + 1; + let mut right = nums.len() - 1; + while left < right { + if let Some(sum) = nums[i] + .checked_add(nums[j]) + .and_then(|s| s.checked_add(nums[left])) + .and_then(|s| s.checked_add(nums[right])) + { + match sum.cmp(&target) { + std::cmp::Ordering::Equal => { + result.push(vec![nums[i], nums[j], nums[left], nums[right]]); + left += 1; + while left < right && nums[left] == nums[left - 1] { + left += 1; + } + } + std::cmp::Ordering::Greater => { + right -= 1; + } + std::cmp::Ordering::Less => { + left += 1; + } + } + } else { + left += 1; + } + } + } + } + + result +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn example_1() { + let nums = vec![1, 0, -1, 0, -2, 2]; + let target = 0; + let expected = vec![vec![-2, -1, 1, 2], vec![-2, 0, 0, 2], vec![-1, 0, 0, 1]]; + assert_eq!(four_sum(nums, target), expected); + } + + #[test] + fn example_2() { + let nums = vec![2, 2, 2, 2, 2]; + let target = 8; + let expected = vec![vec![2, 2, 2, 2]]; + assert_eq!(four_sum(nums, target), expected); + } +} diff --git a/src/letter_combinations_of_a_phone_number.rs b/src/letter_combinations_of_a_phone_number.rs index 1e9fcb7..27be5aa 100644 --- a/src/letter_combinations_of_a_phone_number.rs +++ b/src/letter_combinations_of_a_phone_number.rs @@ -1,3 +1,5 @@ +// refer to https://leetcode.com/problems/letter-combinations-of-a-phone-number/ + pub fn letter_combinations(digits: String) -> Vec { if digits.is_empty() { return vec![]; diff --git a/src/lib.rs b/src/lib.rs index f467c7a..9f707dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,4 +20,5 @@ pub mod best_time_to_buy_and_sell_stock; pub mod maximum_different_between_increasing_elements; pub mod count_submatrices_with_all_ones; pub mod jump_game; -pub mod letter_combinations_of_a_phone_number; \ No newline at end of file +pub mod letter_combinations_of_a_phone_number; +pub mod four_sum; \ No newline at end of file