diff --git a/Cargo.lock b/Cargo.lock index 191f843e..ea91aec0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,10 +295,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "leetcode" -version = "0.1.0" - [[package]] name = "libc" version = "0.2.147" @@ -557,6 +553,10 @@ dependencies = [ "serde_json", ] +[[package]] +name = "two_sum" +version = "0.1.0" + [[package]] name = "unicode-ident" version = "1.0.11" diff --git a/Cargo.toml b/Cargo.toml index fa613706..9b8dbe23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ members = [ "electronics", "financial", "geodesy", - "leetcode", + "leetcode/*.*", "math", "physics", "weiss/c02", diff --git a/leetcode/Cargo.toml b/leetcode/001.two_sum/Cargo.toml similarity index 64% rename from leetcode/Cargo.toml rename to leetcode/001.two_sum/Cargo.toml index 474153f5..a94a8b4d 100644 --- a/leetcode/Cargo.toml +++ b/leetcode/001.two_sum/Cargo.toml @@ -1,7 +1,6 @@ [package] -name = "leetcode" +name = "two_sum" version = "0.1.0" edition = "2021" -publish = false [dependencies] diff --git a/leetcode/src/bin/l01_two_sum.rs b/leetcode/001.two_sum/src/main.rs similarity index 70% rename from leetcode/src/bin/l01_two_sum.rs rename to leetcode/001.two_sum/src/main.rs index f6f624af..390a8b12 100644 --- a/leetcode/src/bin/l01_two_sum.rs +++ b/leetcode/001.two_sum/src/main.rs @@ -7,7 +7,7 @@ fn solution1(nums: &[i32], target: i32) -> Option<(usize, usize)> { for (i, m) in nums.iter().enumerate() { for (j, n) in nums.iter().enumerate() { - if m + n == target { + if i != j && m + n == target { return Some((i, j)); } } @@ -19,4 +19,12 @@ fn main() { let nums = &[2, 7, 11, 15]; let target = 9; assert_eq!(solution1(nums, target), Some((0, 1))); + + let nums = &[3, 2, 4]; + let target = 6; + assert_eq!(solution1(nums, target), Some((1, 2))); + + let nums = &[3, 3]; + let target = 6; + assert_eq!(solution1(nums, target), Some((0, 1))); } diff --git a/leetcode/src/bin/l02_add_two_numbers.rs b/leetcode/src/bin/l02_add_two_numbers.rs new file mode 100644 index 00000000..3eec9925 --- /dev/null +++ b/leetcode/src/bin/l02_add_two_numbers.rs @@ -0,0 +1,83 @@ +// Copyright (c) 2023 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be +// found in the LICENSE file. + +//! Problem: [Add two numbers](https://leetcode.com/problems/add-two-numbers) + +#[derive(Debug, Default, Clone, PartialEq, Eq)] +pub struct ListNode { + pub val: i32, + pub next: ListNodePtr, +} + +pub type ListNodePtr = Option>; + +impl ListNode { + #[must_use] + #[inline] + pub fn new(val: i32) -> Self { + Self { val, next: None } + } + + #[must_use] + #[inline] + pub fn new_ptr(val: i32) -> ListNodePtr { + Some(Box::new(Self::new(val))) + } + + pub fn push_head(head: ListNodePtr, val: i32) -> ListNodePtr { + Some(Box::new(Self { val, next: head })) + } + + pub fn push_tail(tail: &mut ListNodePtr, val: i32) -> ListNodePtr { + debug_assert!(tail.next.is_none()); + let new_tail = Self::new_ptr(val); + tail.next = new_tail; + new_tail + } + + #[must_use] + pub fn from_slice(s: &[i32]) -> ListNodePtr { + assert!(s.len() > 0); + let mut list = None; + for val in s.iter().rev() { + list = Self::push_head(list, *val); + } + list + } +} + +fn solution1(l1: ListNodePtr, l2: ListNodePtr) -> ListNodePtr { + let mut l3 = None; + let div = 0; + let rem = 0; + loop { + l1 = l1.unwrap(); + l2 = l2.unwrap(); + let val1 = l1.value; + l1 = l1.next(); + let val2 = l2.value; + l2 = l2.next(); + (div, rem) = (l1 + l2 + div).div_rem(10); + l3 = ListNode::push_head(l3, rem); + + if l1.is_none() { + l2.value += div; + l3.next = l2; + break; + } + if l2.is_none() { + l1.value += div; + l3.next = l1; + break; + } + } + l3 +} + +fn main() { + let l1 = ListNode::from_slice(&[2, 4, 3]); + let l2 = ListNode::from_slice(&[5, 6, 4]); + let target = ListNode::from_slice(&[7, 0, 8]); + assert_eq!(solution1(l1, l2), target); +} diff --git a/weiss/CMakeLists.txt b/weiss/CMakeLists.txt index 8cb0b239..a9713514 100644 --- a/weiss/CMakeLists.txt +++ b/weiss/CMakeLists.txt @@ -3,3 +3,4 @@ cmake_minimum_required(VERSION 3.10) project(weiss C) add_subdirectory(c03) +add_subdirectory(c07) diff --git a/weiss/c07/CMakeLists.txt b/weiss/c07/CMakeLists.txt new file mode 100644 index 00000000..e69de29b