From 556db15dba4ece6b0c32592bf3684d0179dcad4f Mon Sep 17 00:00:00 2001 From: Guilleag01 Date: Thu, 31 Jul 2025 00:52:40 +0200 Subject: [PATCH] Some leetcode problems --- .gitignore | 1 + Cargo.lock | 7 ++++ Cargo.toml | 6 +++ src/lib.rs | 19 +++++++++ src/p1.rs | 29 +++++++++++++ src/p2.rs | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/p238.rs | 34 +++++++++++++++ 7 files changed, 215 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 src/p1.rs create mode 100644 src/p2.rs create mode 100644 src/p238.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..8779c0d --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "leetcode" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..884c0d3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "leetcode" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5786df9 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,19 @@ +pub mod p1; +pub mod p2; +pub mod p238; + +pub struct Solution; + +// Definition for singly-linked list. +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct ListNode { + pub val: i32, + pub next: Option>, +} + +impl ListNode { + #[inline] + fn _new(val: i32) -> Self { + ListNode { next: None, val } + } +} diff --git a/src/p1.rs b/src/p1.rs new file mode 100644 index 0000000..bc3b229 --- /dev/null +++ b/src/p1.rs @@ -0,0 +1,29 @@ +use crate::Solution; + +impl Solution { + pub fn two_sum(nums: Vec, target: i32) -> Vec { + for (i, e1) in nums.clone().iter().enumerate() { + for (j, e2) in nums[i + 1..].iter().enumerate() { + if e1 + e2 == target { + return vec![i as i32, (i + (j + 1)) as i32]; + } + } + } + unreachable!() // there is exactly one solution + } +} + +#[test] +pub fn test_two_sum_1() { + assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), vec![0, 1]); +} + +#[test] +pub fn test_two_sum_2() { + assert_eq!(Solution::two_sum(vec![3, 2, 4], 6), vec![1, 2]); +} + +#[test] +pub fn test_two_sum_3() { + assert_eq!(Solution::two_sum(vec![3, 3], 6), vec![0, 1]); +} diff --git a/src/p2.rs b/src/p2.rs new file mode 100644 index 0000000..846f311 --- /dev/null +++ b/src/p2.rs @@ -0,0 +1,119 @@ +use crate::{ListNode, Solution}; + +// Definition for singly-linked list. + +impl Solution { + pub fn add_two_numbers( + l1: Option>, + l2: Option>, + ) -> Option> { + Solution::add_two_numbers_rec(l1, l2, 0) + } + + pub fn add_two_numbers_rec( + l1: Option>, + l2: Option>, + carry: i32, + ) -> Option> { + Option::Some(Box::new(match (l1, l2) { + (None, None) => { + if carry > 0 { + ListNode { + val: 1, + next: Option::None, + } + } else { + return Option::None; + } + } + (None, Some(a)) => { + let mut sum = a.val + carry; + let mut new_carry = 0; + if sum > 9 { + new_carry = 1; + sum -= 10; + } + ListNode { + val: sum, + next: Solution::add_two_numbers_rec(Option::None, a.next, new_carry), + } + } + (Some(a), None) => { + let mut sum = a.val + carry; + let mut new_carry = 0; + if sum > 9 { + new_carry = 1; + sum -= 10; + } + ListNode { + val: sum, + next: Solution::add_two_numbers_rec(a.next, Option::None, new_carry), + } + } + (Some(a), Some(b)) => { + let mut sum = a.val + b.val + carry; + let mut new_carry = 0; + if sum > 9 { + new_carry = 1; + sum -= 10; + } + ListNode { + val: sum, + next: Solution::add_two_numbers_rec(a.next, b.next, new_carry), + } + } + })) + } +} + +#[test] +pub fn test_add_two_numbers_1() { + assert_eq!( + i32_from_list_node(Solution::add_two_numbers( + list_node_from_array(&[2, 4, 3]), + list_node_from_array(&[5, 6, 4]) + )), + 807 + ); +} + +#[test] +pub fn test_add_two_numbers_2() { + assert_eq!( + i32_from_list_node(Solution::add_two_numbers( + list_node_from_array(&[0]), + list_node_from_array(&[0]) + )), + 0 + ); +} + +#[test] +pub fn test_add_two_numbers_3() { + assert_eq!( + i32_from_list_node(Solution::add_two_numbers( + list_node_from_array(&[9, 9, 9, 9, 9, 9, 9]), + list_node_from_array(&[9, 9, 9, 9]) + )), + 10009998 + ); +} + +pub fn list_node_from_array(arr: &[i32]) -> Option> { + Option::Some(Box::new(ListNode { + val: arr[0], + next: if arr.len() > 1 { + list_node_from_array(&arr[1..]) + } else { + Option::None + }, + })) +} + +pub fn i32_from_list_node(l: Option>) -> i32 { + if let Some(a) = l { + a.val + i32_from_list_node(a.next) * 10 + } else { + 0 + } +} diff --git a/src/p238.rs b/src/p238.rs new file mode 100644 index 0000000..c09f0e6 --- /dev/null +++ b/src/p238.rs @@ -0,0 +1,34 @@ +use crate::Solution; + +impl Solution { + pub fn product_except_self(nums: Vec) -> Vec { + let mut res = nums.clone(); + let mut acc = 1; + for i in 0..nums.len() - 1 { + acc *= nums[i]; + res[i + 1] = acc; + } + acc = 1; + for i in (1..nums.len()).rev() { + acc *= nums[i]; + res[i - 1] *= acc; + } + res + } +} + +#[test] +pub fn test_product_except_self_1() { + assert_eq!( + Solution::product_except_self(vec![1, 2, 3, 4]), + vec![24, 12, 8, 6] + ); +} + +#[test] +pub fn test_product_except_self_2() { + assert_eq!( + Solution::product_except_self(vec![-1, 1, 0, -3, 3]), + vec![0, 0, 9, 0, 0] + ); +}