refactor rust problems and finish p104 and p108
This commit is contained in:
parent
5e6299439c
commit
3b805a6422
@ -3,4 +3,7 @@ name = "leetcode_rs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[workspace]
|
||||
members = ["problems/*"]
|
||||
|
||||
[dependencies]
|
||||
|
6
leetcode_rs/problems/p104/Cargo.toml
Normal file
6
leetcode_rs/problems/p104/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "p104"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
106
leetcode_rs/problems/p104/src/main.rs
Normal file
106
leetcode_rs/problems/p104/src/main.rs
Normal file
@ -0,0 +1,106 @@
|
||||
// Definition for a binary tree node.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
#[inline]
|
||||
pub fn new(val: i32) -> Self {
|
||||
TreeNode {
|
||||
val,
|
||||
left: None,
|
||||
right: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
// 定义Solution结构体
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
match root {
|
||||
None => 0,
|
||||
Some(node) => {
|
||||
let left = Self::max_depth(node.borrow().left.clone());
|
||||
let right = Self::max_depth(node.borrow().right.clone());
|
||||
1 + left.max(right)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建树的辅助函数
|
||||
fn create_tree(values: Vec<Option<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
if values.is_empty() || values[0].is_none() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let root = Rc::new(RefCell::new(TreeNode::new(values[0].unwrap())));
|
||||
let mut queue = std::collections::VecDeque::new();
|
||||
queue.push_back(Rc::clone(&root));
|
||||
|
||||
let mut i = 1;
|
||||
while i < values.len() && !queue.is_empty() {
|
||||
let node = queue.pop_front().unwrap();
|
||||
|
||||
// 左子节点
|
||||
if i < values.len() {
|
||||
if let Some(val) = values[i] {
|
||||
let left = Rc::new(RefCell::new(TreeNode::new(val)));
|
||||
node.borrow_mut().left = Some(Rc::clone(&left));
|
||||
queue.push_back(left);
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
|
||||
// 右子节点
|
||||
if i < values.len() {
|
||||
if let Some(val) = values[i] {
|
||||
let right = Rc::new(RefCell::new(TreeNode::new(val)));
|
||||
node.borrow_mut().right = Some(Rc::clone(&right));
|
||||
queue.push_back(right);
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
Some(root)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// 测试用例1: [3,9,20,null,null,15,7]
|
||||
let test1 = create_tree(vec![
|
||||
Some(3),
|
||||
Some(9),
|
||||
Some(20),
|
||||
None,
|
||||
None,
|
||||
Some(15),
|
||||
Some(7),
|
||||
]);
|
||||
println!(
|
||||
"Test Case 1 - Expected: 3, Got: {}",
|
||||
Solution::max_depth(test1)
|
||||
);
|
||||
|
||||
// 测试用例2: [1,null,2]
|
||||
let test2 = create_tree(vec![Some(1), None, Some(2)]);
|
||||
println!(
|
||||
"Test Case 2 - Expected: 2, Got: {}",
|
||||
Solution::max_depth(test2)
|
||||
);
|
||||
|
||||
// 测试用例3: []
|
||||
let test3 = create_tree(vec![]);
|
||||
println!(
|
||||
"Test Case 3 - Expected: 0, Got: {}",
|
||||
Solution::max_depth(test3)
|
||||
);
|
||||
|
||||
// 你可以添加更多测试用例
|
||||
}
|
6
leetcode_rs/problems/p108/Cargo.toml
Normal file
6
leetcode_rs/problems/p108/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "p108"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
46
leetcode_rs/problems/p108/src/main.rs
Normal file
46
leetcode_rs/problems/p108/src/main.rs
Normal file
@ -0,0 +1,46 @@
|
||||
// Definition for a binary tree node.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
#[inline]
|
||||
pub fn new(val: i32) -> Self {
|
||||
TreeNode {
|
||||
val,
|
||||
left: None,
|
||||
right: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
let length = nums.len();
|
||||
if length == 0 {
|
||||
return None;
|
||||
}
|
||||
let mid = length / 2;
|
||||
let root_val = *nums.get(mid).unwrap();
|
||||
|
||||
let root = Rc::new(RefCell::new(TreeNode::new(root_val)));
|
||||
let left = self::Solution::sorted_array_to_bst(nums[..mid].to_vec());
|
||||
let right = self::Solution::sorted_array_to_bst(nums[mid + 1..].to_vec());
|
||||
root.borrow_mut().left = left;
|
||||
root.borrow_mut().right = right;
|
||||
Some(root)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let test1 = [-10, -3, 0, 5, 9];
|
||||
let node = Solution::sorted_array_to_bst(test1.to_vec());
|
||||
println!("{:?}", node);
|
||||
}
|
@ -1,14 +1,2 @@
|
||||
// src/main.rs
|
||||
|
||||
mod problem_1 {
|
||||
pub mod solution;
|
||||
}
|
||||
|
||||
use problem_1::solution::Solution;
|
||||
|
||||
fn main() {
|
||||
let nums = vec![2, 7, 11, 15];
|
||||
let target = 9;
|
||||
let result = Solution::two_sum(nums, target);
|
||||
println!("{:?}", result); // 输出: [0, 1]
|
||||
}
|
||||
|
@ -1,32 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Solution;
|
||||
|
||||
impl Solution {
|
||||
/// This function takes a vector of integers (`nums`) and an integer (`target`) as input.
|
||||
/// It returns a vector of integers.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `nums` - A vector of integers.
|
||||
/// * `target` - An integer that represents the target sum.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// This function returns a vector of integers.
|
||||
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
|
||||
let mut map = HashMap::new();
|
||||
|
||||
for (i, &num) in nums.iter().enumerate() {
|
||||
let complement = target - num;
|
||||
|
||||
if let Some(&index) = map.get(&complement) {
|
||||
return vec![index as i32, i as i32];
|
||||
}
|
||||
|
||||
map.insert(num, i);
|
||||
}
|
||||
|
||||
vec![]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user