Skip to content

Commit

Permalink
lc: Move to separated crates
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Nov 14, 2023
1 parent 1bf58e8 commit d8f7a9a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ members = [
"electronics",
"financial",
"geodesy",
"leetcode",
"leetcode/*.*",
"math",
"physics",
"weiss/c02",
Expand Down
3 changes: 1 addition & 2 deletions leetcode/Cargo.toml → leetcode/001.two_sum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "leetcode"
name = "two_sum"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand All @@ -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)));
}
83 changes: 83 additions & 0 deletions leetcode/src/bin/l02_add_two_numbers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2023 Xu Shaohua <[email protected]>. 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<Box<ListNode>>;

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);
}
1 change: 1 addition & 0 deletions weiss/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ cmake_minimum_required(VERSION 3.10)
project(weiss C)

add_subdirectory(c03)
add_subdirectory(c07)
Empty file added weiss/c07/CMakeLists.txt
Empty file.

0 comments on commit d8f7a9a

Please sign in to comment.