From 341903a81c34c87a410d9905be2bf81a80cb7e90 Mon Sep 17 00:00:00 2001 From: finchxx <1470073710@qq.com> Date: Fri, 31 Jan 2025 11:12:03 +0800 Subject: [PATCH] update --- exercises/algorithm/algorithm1.rs | 42 ++++++++++++++++++----- exercises/algorithm/algorithm2.rs | 20 ++++++++++- exercises/algorithm/algorithm3.rs | 16 +++++++-- exercises/algorithm/algorithm4.rs | 55 +++++++++++++++++++++++++++---- 4 files changed, 115 insertions(+), 18 deletions(-) diff --git a/exercises/algorithm/algorithm1.rs b/exercises/algorithm/algorithm1.rs index f7a99bf02..356e4eb21 100644 --- a/exercises/algorithm/algorithm1.rs +++ b/exercises/algorithm/algorithm1.rs @@ -2,11 +2,10 @@ single linked list merge This problem requires you to merge two ordered singly linked lists into one ordered singly linked list */ -// I AM NOT DONE use std::fmt::{self, Display, Formatter}; use std::ptr::NonNull; -use std::vec::*; +use std::{default, vec::*}; #[derive(Debug)] struct Node { @@ -69,14 +68,41 @@ impl LinkedList { }, } } - pub fn merge(list_a:LinkedList,list_b:LinkedList) -> Self + pub fn merge(mut list_a:LinkedList,mut list_b:LinkedList) -> Self where T: PartialOrd + Clone { //TODO - Self { - length: 0, - start: None, - end: None, - } + // let common = if list_a.length >= list_b.length {list_a.length} else {list_b.length}; + // let lens = list_a.length + list_b.length; + let mut result = LinkedList::new(); + let mut i = 0; + let mut j = 0; + let len_a = list_a.length as i32; + let len_b = list_b.length as i32; + + + while i < len_a && j < len_b { + let a = list_a.get(i).unwrap(); + let b = list_b.get(j).unwrap(); + + if a <= b { + result.add(a.clone()); + i += 1; + } else { + result.add(b.clone()); + j += 1; + } + }; + while i < len_a { + result.add(list_a.get(i).unwrap().clone()); + i += 1; + }; + + while j < len_b { + result.add(list_b.get(j).unwrap().clone()); + j += 1; + }; + + result } } diff --git a/exercises/algorithm/algorithm2.rs b/exercises/algorithm/algorithm2.rs index 08720ff44..967d119d6 100644 --- a/exercises/algorithm/algorithm2.rs +++ b/exercises/algorithm/algorithm2.rs @@ -2,7 +2,6 @@ double linked list reverse This problem requires you to reverse a doubly linked list */ -// I AM NOT DONE use std::fmt::{self, Display, Formatter}; use std::ptr::NonNull; @@ -74,6 +73,25 @@ impl LinkedList { } pub fn reverse(&mut self){ // TODO + let mut last_node = self.end; + self.start = last_node; + while let Some(mut current_node) = last_node { + + let prev_node = unsafe {(*current_node.as_ptr()).next}; + let next_node = unsafe {(*current_node.as_ptr()).prev}; + // let prev_prev_node = unsafe {(*prev_node.unwrap().as_ptr()).prev}; + unsafe { + (*current_node.as_ptr()).next = next_node; + (*current_node.as_ptr()).prev = prev_node; + last_node = next_node; + } + if let None = next_node { + self.end = last_node; + break; + } + }; + + } } diff --git a/exercises/algorithm/algorithm3.rs b/exercises/algorithm/algorithm3.rs index 37878d6a7..12031c8d1 100644 --- a/exercises/algorithm/algorithm3.rs +++ b/exercises/algorithm/algorithm3.rs @@ -3,11 +3,21 @@ This problem requires you to implement a sorting algorithm you can use bubble sorting, insertion sorting, heap sorting, etc. */ -// I AM NOT DONE -fn sort(array: &mut [T]){ - //TODO +use std::mem::swap; + +fn sort(array: &mut [T]) where T: PartialEq + PartialOrd{ + for i in 0..array.len() { + for j in i..array.len() { + if array[i] > array[j] { + array.swap(i, j); + } + } + } } + + + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/algorithm/algorithm4.rs b/exercises/algorithm/algorithm4.rs index 271b772c5..b10a5e1ac 100644 --- a/exercises/algorithm/algorithm4.rs +++ b/exercises/algorithm/algorithm4.rs @@ -3,9 +3,7 @@ This problem requires you to implement a basic interface for a binary tree */ -//I AM NOT DONE -use std::cmp::Ordering; -use std::fmt::Debug; +use std::{cmp::Ordering, fmt::Debug}; #[derive(Debug)] @@ -50,13 +48,19 @@ where // Insert a value into the BST fn insert(&mut self, value: T) { - //TODO + match &mut self.root { + Some(node) => node.insert(value), + None => self.root = Some(Box::new(TreeNode::new(value))), + } } // Search for a value in the BST fn search(&self, value: T) -> bool { - //TODO - true + match &self.root { + Some(node) => node.search(&value), + _ => false + } + } } @@ -67,6 +71,45 @@ where // Insert a node into the tree fn insert(&mut self, value: T) { //TODO + match value.cmp(&self.value) { + Ordering::Less => { + if let Some(left) = &mut self.left { + left.insert(value); + } else { + self.left = Some(Box::new(TreeNode::new(value))); + } + } + Ordering::Greater => { + if let Some(right) = &mut self.right { + right.insert(value); + } else { + self.right = Some(Box::new(TreeNode::new(value))); + } + } + Ordering::Equal => { + // 重复值不插入 + } + } + } + + fn search(&self, target: &T) -> bool { // 使用引用避免所有权转移 + match target.cmp(&self.value) { + Ordering::Equal => true, // 找到匹配值 + Ordering::Less => { // 搜索左子树 + if let Some(left) = &self.left { + left.search(target) + } else { + false + } + } + Ordering::Greater => { // 搜索右子树 + if let Some(right) = &self.right { + right.search(target) + } else { + false + } + } + } } }