mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	zig : upgrade codes && rust : add codes for chapter_searching and chapter_dynamic_programming. (#591)
* zig : update zig codes * rust : add codes for linear_search and hashing_search * rust : add codes for linear_search and hashing_search * rust : add codes for chapter_dynamic_programming
This commit is contained in:
		@ -154,5 +154,45 @@ path = "chapter_tree/binary_tree_dfs.rs"
 | 
			
		||||
name = "binary_tree"
 | 
			
		||||
path = "chapter_tree/binary_tree.rs"
 | 
			
		||||
 | 
			
		||||
# Run Command: cargo run --bin linear_search
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "linear_search"
 | 
			
		||||
path = "chapter_searching/linear_search.rs"
 | 
			
		||||
 | 
			
		||||
# Run Command: cargo run --bin hashing_search
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "hashing_search"
 | 
			
		||||
path = "chapter_searching/hashing_search.rs"
 | 
			
		||||
 | 
			
		||||
# Run Command: cargo run --bin climbing_stairs_dfs
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "climbing_stairs_dfs"
 | 
			
		||||
path = "chapter_dynamic_programming/climbing_stairs_dfs.rs"
 | 
			
		||||
 | 
			
		||||
# Run Command: cargo run --bin climbing_stairs_dfs_mem
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "climbing_stairs_dfs_mem"
 | 
			
		||||
path = "chapter_dynamic_programming/climbing_stairs_dfs_mem.rs"
 | 
			
		||||
 | 
			
		||||
# Run Command: cargo run --bin climbing_stairs_dp
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "climbing_stairs_dp"
 | 
			
		||||
path = "chapter_dynamic_programming/climbing_stairs_dp.rs"
 | 
			
		||||
 | 
			
		||||
# Run Command: cargo run --bin min_cost_climbing_stairs_dp
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "min_cost_climbing_stairs_dp"
 | 
			
		||||
path = "chapter_dynamic_programming/min_cost_climbing_stairs_dp.rs"
 | 
			
		||||
 | 
			
		||||
# Run Command: cargo run --bin climbing_stairs_constraint_dp
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "climbing_stairs_constraint_dp"
 | 
			
		||||
path = "chapter_dynamic_programming/climbing_stairs_constraint_dp.rs"
 | 
			
		||||
 | 
			
		||||
# Run Command: cargo run --bin climbing_stairs_backtrack
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "climbing_stairs_backtrack"
 | 
			
		||||
path = "chapter_dynamic_programming/climbing_stairs_backtrack.rs"
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
rand = "0.8.5"
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,37 @@
 | 
			
		||||
/*
 | 
			
		||||
 * File: climbing_stairs_backtrack.rs
 | 
			
		||||
 * Created Time: 2023-07-09
 | 
			
		||||
 * Author: sjinzh (sjinzh@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 回溯 */
 | 
			
		||||
fn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {
 | 
			
		||||
    // 当爬到第 n 阶时,方案数量加 1
 | 
			
		||||
    if state == n { res[0] = res[0] + 1; }
 | 
			
		||||
    // 遍历所有选择
 | 
			
		||||
    for &choice in choices {
 | 
			
		||||
        // 剪枝:不允许越过第 n 阶
 | 
			
		||||
        if state + choice > n { break; }
 | 
			
		||||
        // 尝试:做出选择,更新状态
 | 
			
		||||
        backtrack(choices, state + choice, n, res);
 | 
			
		||||
        // 回退
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 爬楼梯:回溯 */
 | 
			
		||||
fn climbing_stairs_backtrack(n: usize) -> i32 {
 | 
			
		||||
    let choices = vec![ 1, 2 ]; // 可选择向上爬 1 或 2 阶
 | 
			
		||||
    let state = 0; // 从第 0 阶开始爬
 | 
			
		||||
    let mut res = Vec::new();
 | 
			
		||||
    res.push(0); // 使用 res[0] 记录方案数量
 | 
			
		||||
    backtrack(&choices, state, n as i32, &mut res);
 | 
			
		||||
    res[0]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
pub fn main() {
 | 
			
		||||
    let n: usize = 9;
 | 
			
		||||
 | 
			
		||||
    let res = climbing_stairs_backtrack(n);
 | 
			
		||||
    println!("爬 {n} 阶楼梯共有 {res} 种方案");
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,31 @@
 | 
			
		||||
/*
 | 
			
		||||
 * File: climbing_stairs_constraint_dp.rs
 | 
			
		||||
 * Created Time: 2023-07-09
 | 
			
		||||
 * Author: sjinzh (sjinzh@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 带约束爬楼梯:动态规划 */
 | 
			
		||||
fn climbing_stairs_constraint_dp(n: usize) -> i32 {
 | 
			
		||||
    if n == 1 || n == 2 { return n as i32 };
 | 
			
		||||
    // 初始化 dp 表,用于存储子问题的解
 | 
			
		||||
    let mut dp = vec![vec![-1; 3]; n + 1];
 | 
			
		||||
    // 初始状态:预设最小子问题的解
 | 
			
		||||
    dp[1][1] = 1;
 | 
			
		||||
    dp[1][2] = 0;
 | 
			
		||||
    dp[2][1] = 0;
 | 
			
		||||
    dp[2][2] = 1;
 | 
			
		||||
    // 状态转移:从较小子问题逐步求解较大子问题
 | 
			
		||||
    for i in 3..=n {
 | 
			
		||||
        dp[i][1] = dp[i - 1][2];
 | 
			
		||||
        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];
 | 
			
		||||
    }
 | 
			
		||||
    dp[n][1] + dp[n][2]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
pub fn main() {
 | 
			
		||||
    let n: usize = 9;
 | 
			
		||||
 | 
			
		||||
    let res = climbing_stairs_constraint_dp(n);
 | 
			
		||||
    println!("爬 {n} 阶楼梯共有 {res} 种方案");
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,27 @@
 | 
			
		||||
/*
 | 
			
		||||
 * File: climbing_stairs_dfs.rs
 | 
			
		||||
 * Created Time: 2023-07-09
 | 
			
		||||
 * Author: sjinzh (sjinzh@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 搜索 */
 | 
			
		||||
fn dfs(i: usize) -> i32 {
 | 
			
		||||
    // 已知 dp[1] 和 dp[2] ,返回之
 | 
			
		||||
    if i == 1 || i == 2 { return i as i32 };
 | 
			
		||||
    // dp[i] = dp[i-1] + dp[i-2]
 | 
			
		||||
    let count = dfs(i - 1) + dfs(i - 2);
 | 
			
		||||
    count
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 爬楼梯:搜索 */
 | 
			
		||||
fn climbing_stairs_dfs(n: usize) -> i32 {
 | 
			
		||||
    dfs(n) 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
pub fn main() {
 | 
			
		||||
    let n: usize = 9;
 | 
			
		||||
 | 
			
		||||
    let res = climbing_stairs_dfs(n);
 | 
			
		||||
    println!("爬 {n} 阶楼梯共有 {res} 种方案");
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,33 @@
 | 
			
		||||
/*
 | 
			
		||||
 * File: climbing_stairs_dfs_mem.rs
 | 
			
		||||
 * Created Time: 2023-07-09
 | 
			
		||||
 * Author: sjinzh (sjinzh@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 记忆化搜索 */
 | 
			
		||||
fn dfs(i: usize, mem: &mut [i32]) -> i32 {
 | 
			
		||||
    // 已知 dp[1] 和 dp[2] ,返回之
 | 
			
		||||
    if i == 1 || i == 2 { return i as i32};
 | 
			
		||||
    // 若存在记录 dp[i] ,则直接返回之
 | 
			
		||||
    if mem[i] != -1 { return mem[i] };
 | 
			
		||||
    // dp[i] = dp[i-1] + dp[i-2]
 | 
			
		||||
    let count = dfs(i - 1, mem) + dfs(i - 2, mem);
 | 
			
		||||
    // 记录 dp[i]
 | 
			
		||||
    mem[i] = count;
 | 
			
		||||
    count
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 爬楼梯:记忆化搜索 */
 | 
			
		||||
fn climbing_stairs_dfs_mem(n: usize) -> i32 {
 | 
			
		||||
    // mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
 | 
			
		||||
    let mut mem = vec![-1; n + 1];
 | 
			
		||||
    dfs(n, &mut mem)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
pub fn main() {
 | 
			
		||||
    let n: usize = 9;
 | 
			
		||||
 | 
			
		||||
    let res = climbing_stairs_dfs_mem(n);
 | 
			
		||||
    println!("爬 {n} 阶楼梯共有 {res} 种方案");
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								codes/rust/chapter_dynamic_programming/climbing_stairs_dp.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								codes/rust/chapter_dynamic_programming/climbing_stairs_dp.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,44 @@
 | 
			
		||||
/*
 | 
			
		||||
 * File: climbing_stairs_dp.rs
 | 
			
		||||
 * Created Time: 2023-07-09
 | 
			
		||||
 * Author: sjinzh (sjinzh@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 爬楼梯:动态规划 */
 | 
			
		||||
fn climbing_stairs_dp(n: usize) -> i32 {
 | 
			
		||||
    // 已知 dp[1] 和 dp[2] ,返回之
 | 
			
		||||
    if n == 1 || n == 2 { return n as i32 };
 | 
			
		||||
    // 初始化 dp 表,用于存储子问题的解
 | 
			
		||||
    let mut dp = vec![-1; n + 1];
 | 
			
		||||
    // 初始状态:预设最小子问题的解
 | 
			
		||||
    dp[1] = 1;
 | 
			
		||||
    dp[2] = 2;
 | 
			
		||||
    // 状态转移:从较小子问题逐步求解较大子问题
 | 
			
		||||
    for i in 3..=n {
 | 
			
		||||
        dp[i] = dp[i - 1] + dp[i - 2];
 | 
			
		||||
    }
 | 
			
		||||
    dp[n]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 爬楼梯:状态压缩后的动态规划 */
 | 
			
		||||
fn climbing_stairs_dp_comp(n: usize) -> i32 {
 | 
			
		||||
    if n == 1 || n == 2 { return n as i32 };
 | 
			
		||||
    let (mut a, mut b) = (1, 2);
 | 
			
		||||
    for _ in 3..=n {
 | 
			
		||||
        let tmp = b;
 | 
			
		||||
        b = a + b;
 | 
			
		||||
        a = tmp;
 | 
			
		||||
    }
 | 
			
		||||
    b
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
pub fn main() {
 | 
			
		||||
    let n: usize = 9;
 | 
			
		||||
 | 
			
		||||
    let res = climbing_stairs_dp(n);
 | 
			
		||||
    println!("爬 {n} 阶楼梯共有 {res} 种方案");
 | 
			
		||||
 | 
			
		||||
    let res = climbing_stairs_dp_comp(n);
 | 
			
		||||
    println!("爬 {n} 阶楼梯共有 {res} 种方案");
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,48 @@
 | 
			
		||||
/*
 | 
			
		||||
 * File: min_cost_climbing_stairs_dp.rs
 | 
			
		||||
 * Created Time: 2023-07-09
 | 
			
		||||
 * Author: sjinzh (sjinzh@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
 use std::cmp;
 | 
			
		||||
 | 
			
		||||
/* 爬楼梯最小代价:动态规划 */
 | 
			
		||||
fn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {
 | 
			
		||||
    let n = cost.len() - 1;
 | 
			
		||||
    if n == 1 || n == 2 { return cost[n] };
 | 
			
		||||
    // 初始化 dp 表,用于存储子问题的解
 | 
			
		||||
    let mut dp = vec![-1; n + 1];
 | 
			
		||||
    // 初始状态:预设最小子问题的解
 | 
			
		||||
    dp[1] = cost[1];
 | 
			
		||||
    dp[2] = cost[2];
 | 
			
		||||
    // 状态转移:从较小子问题逐步求解较大子问题
 | 
			
		||||
    for i in 3..=n {
 | 
			
		||||
        dp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];
 | 
			
		||||
    }
 | 
			
		||||
    dp[n]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 爬楼梯最小代价:状态压缩后的动态规划 */
 | 
			
		||||
fn min_cost_climbing_stairs_dp_comp(cost: &[i32]) -> i32 {
 | 
			
		||||
    let n = cost.len() - 1;
 | 
			
		||||
    if n == 1 || n == 2 { return cost[n] };
 | 
			
		||||
    let (mut a, mut b) = (cost[1], cost[2]);
 | 
			
		||||
    for i in 3..=n {
 | 
			
		||||
        let tmp = b;
 | 
			
		||||
        b = cmp::min(a, tmp) + cost[i];
 | 
			
		||||
        a = tmp;
 | 
			
		||||
    }
 | 
			
		||||
    b
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
pub fn main() {
 | 
			
		||||
    let cost = [ 0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1 ];
 | 
			
		||||
    println!("输入楼梯的代价列表为 {:?}", &cost);
 | 
			
		||||
 | 
			
		||||
    let res = min_cost_climbing_stairs_dp(&cost);
 | 
			
		||||
    println!("爬完楼梯的最低代价为 {res}");
 | 
			
		||||
 | 
			
		||||
    let res = min_cost_climbing_stairs_dp_comp(&cost);
 | 
			
		||||
    println!("爬完楼梯的最低代价为 {res}");
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										49
									
								
								codes/rust/chapter_searching/hashing_search.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								codes/rust/chapter_searching/hashing_search.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,49 @@
 | 
			
		||||
/*
 | 
			
		||||
 * File: hashing_search.rs
 | 
			
		||||
 * Created Time: 2023-07-09
 | 
			
		||||
 * Author: sjinzh (sjinzh@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
 include!("../include/include.rs");
 | 
			
		||||
 | 
			
		||||
 use std::collections::HashMap;
 | 
			
		||||
 use std::rc::Rc;
 | 
			
		||||
 use std::cell::RefCell;
 | 
			
		||||
 use list_node::ListNode;
 | 
			
		||||
 | 
			
		||||
/* 哈希查找(数组) */
 | 
			
		||||
fn hashing_search_array<'a>(map: &'a HashMap<i32, usize>, target: i32) -> Option<&'a usize> {
 | 
			
		||||
    // 哈希表的 key: 目标元素,value: 索引
 | 
			
		||||
    // 若哈希表中无此 key ,返回 None
 | 
			
		||||
    map.get(&target)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 哈希查找(链表) */
 | 
			
		||||
fn hashing_search_linked_list(map: &HashMap<i32, Rc<RefCell<ListNode<i32>>>>, target: i32) -> Option<&Rc<RefCell<ListNode<i32>>>> {
 | 
			
		||||
    // 哈希表的 key: 目标节点值,value: 节点对象
 | 
			
		||||
    // 若哈希表中无此 key ,返回 None
 | 
			
		||||
    map.get(&target)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
pub fn main() {
 | 
			
		||||
    let target = 3;
 | 
			
		||||
 | 
			
		||||
    /* 哈希查找(数组) */
 | 
			
		||||
    let nums = [ 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 ];
 | 
			
		||||
    // 初始化哈希表
 | 
			
		||||
    let mut map = HashMap::new();
 | 
			
		||||
    for (i, num) in nums.iter().enumerate() {
 | 
			
		||||
        map.insert(*num, i);
 | 
			
		||||
    }
 | 
			
		||||
    let index = hashing_search_array(&map, target);
 | 
			
		||||
    println!("目标元素 3 的索引 = {}", index.unwrap());
 | 
			
		||||
 | 
			
		||||
    /* 哈希查找(链表) */
 | 
			
		||||
    let head = ListNode::arr_to_linked_list(&nums);
 | 
			
		||||
    // 初始化哈希表
 | 
			
		||||
    // let mut map1 = HashMap::new();
 | 
			
		||||
    let map1 = ListNode::linked_list_to_hashmap(head);
 | 
			
		||||
    let node = hashing_search_linked_list(&map1, target);
 | 
			
		||||
    println!("目标节点值 3 的对应节点对象为 {:?}", node);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										49
									
								
								codes/rust/chapter_searching/linear_search.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								codes/rust/chapter_searching/linear_search.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,49 @@
 | 
			
		||||
/*
 | 
			
		||||
 * File: linear_search.rs
 | 
			
		||||
 * Created Time: 2023-07-09
 | 
			
		||||
 * Author: sjinzh (sjinzh@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
 include!("../include/include.rs");
 | 
			
		||||
 | 
			
		||||
 use std::rc::Rc;
 | 
			
		||||
 use std::cell::RefCell;
 | 
			
		||||
 use list_node::ListNode;
 | 
			
		||||
 | 
			
		||||
/* 线性查找(数组) */
 | 
			
		||||
fn linear_search_array(nums: &[i32], target: i32) -> i32 {
 | 
			
		||||
    // 遍历数组
 | 
			
		||||
    for (i, num) in nums.iter().enumerate() {
 | 
			
		||||
        if num == &target {
 | 
			
		||||
            return i as i32;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 未找到目标元素,返回 -1
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 线性查找(链表) */
 | 
			
		||||
fn linear_search_linked_list(head: Rc<RefCell<ListNode<i32>>>, target: i32) -> Option<Rc<RefCell<ListNode<i32>>>> {
 | 
			
		||||
    // 找到目标节点,返回之
 | 
			
		||||
    if head.borrow().val == target {return Some(head)};
 | 
			
		||||
    if let Some(node) = &head.borrow_mut().next {
 | 
			
		||||
        return linear_search_linked_list(node.clone(), target);
 | 
			
		||||
    }
 | 
			
		||||
    // 未找到目标节点,返回 None
 | 
			
		||||
    return None;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
pub fn main() {
 | 
			
		||||
    let target = 3;
 | 
			
		||||
 | 
			
		||||
    /* 在数组中执行线性查找 */
 | 
			
		||||
    let nums = [ 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 ];
 | 
			
		||||
    let index = linear_search_array(&nums, target);
 | 
			
		||||
    println!("目标元素 3 的索引 = {}", index);
 | 
			
		||||
 | 
			
		||||
    /* 在链表中执行线性查找 */
 | 
			
		||||
    let head = ListNode::arr_to_linked_list(&nums);
 | 
			
		||||
    let node = linear_search_linked_list(head.unwrap(), target);
 | 
			
		||||
    println!("目标节点值 3 的对应节点对象为 {:?}", node);
 | 
			
		||||
}
 | 
			
		||||
@ -6,7 +6,9 @@
 | 
			
		||||
 | 
			
		||||
use std::rc::Rc;
 | 
			
		||||
use std::cell::RefCell;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct ListNode<T> {
 | 
			
		||||
    pub val: T,
 | 
			
		||||
    pub next: Option<Rc<RefCell<ListNode<T>>>>,
 | 
			
		||||
@ -19,4 +21,41 @@ impl<T> ListNode<T> {
 | 
			
		||||
            next: None,
 | 
			
		||||
        }))
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /* Generate a linked list with an array */
 | 
			
		||||
    pub fn arr_to_linked_list(array: &[T]) -> Option<Rc<RefCell<ListNode<T>>>> 
 | 
			
		||||
    where
 | 
			
		||||
        T: Copy + Clone,
 | 
			
		||||
    {
 | 
			
		||||
        let mut head = None;
 | 
			
		||||
        let mut prev = None;
 | 
			
		||||
        for item in array.iter().rev() {
 | 
			
		||||
            let node = Rc::new(RefCell::new(ListNode {
 | 
			
		||||
                val: *item,
 | 
			
		||||
                next: prev.take(),
 | 
			
		||||
            }));
 | 
			
		||||
            prev = Some(node.clone());
 | 
			
		||||
            head = Some(node);
 | 
			
		||||
        }
 | 
			
		||||
        head
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Generate a hashmap with a linked_list */
 | 
			
		||||
    pub fn linked_list_to_hashmap(
 | 
			
		||||
        linked_list: Option<Rc<RefCell<ListNode<T>>>>,
 | 
			
		||||
    ) -> HashMap<T, Rc<RefCell<ListNode<T>>>> 
 | 
			
		||||
    where
 | 
			
		||||
        T: std::hash::Hash + Eq + Copy + Clone
 | 
			
		||||
    {
 | 
			
		||||
        let mut hashmap = HashMap::new();
 | 
			
		||||
        if let Some(node) = linked_list {
 | 
			
		||||
            let mut current = Some(node.clone());
 | 
			
		||||
            while let Some(cur) = current {
 | 
			
		||||
                let borrow = cur.borrow();
 | 
			
		||||
                hashmap.insert(borrow.val.clone(), cur.clone());
 | 
			
		||||
                current = borrow.next.clone();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        hashmap
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										2
									
								
								codes/zig/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								codes/zig/.gitignore
									
									
									
									
										vendored
									
									
								
							@ -1,2 +1,2 @@
 | 
			
		||||
zig-cache/
 | 
			
		||||
zig-out/
 | 
			
		||||
zig-cache/
 | 
			
		||||
@ -4,244 +4,145 @@
 | 
			
		||||
 | 
			
		||||
const std = @import("std");
 | 
			
		||||
 | 
			
		||||
// Zig Version: 0.11.0-dev.3379+629f0d23b
 | 
			
		||||
// Build Command: zig build
 | 
			
		||||
// Zig Version: 0.11.0-dev.3944+2e424e019
 | 
			
		||||
// Zig Build Command:  zig build -Doptimize=ReleaseFast
 | 
			
		||||
// Zig Run Command:    zig build run_* -Doptimize=ReleaseFast
 | 
			
		||||
pub fn build(b: *std.Build) void {
 | 
			
		||||
    const target = b.standardTargetOptions(.{});
 | 
			
		||||
    const optimize = b.standardOptimizeOption(.{});
 | 
			
		||||
 | 
			
		||||
    const group_name_path = .{
 | 
			
		||||
        // Source File: "chapter_computational_complexity/time_complexity.zig"
 | 
			
		||||
        // Run Command: zig build run_time_complexity
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "time_complexity", 
 | 
			
		||||
            .path = "chapter_computational_complexity/time_complexity.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_time_complexity -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "time_complexity", .path = "chapter_computational_complexity/time_complexity.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_computational_complexity/worst_best_time_complexity.zig"
 | 
			
		||||
        // Run Command: zig build run_worst_best_time_complexity
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "worst_best_time_complexity", 
 | 
			
		||||
            .path = "chapter_computational_complexity/worst_best_time_complexity.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_worst_best_time_complexity -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "worst_best_time_complexity", .path = "chapter_computational_complexity/worst_best_time_complexity.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_computational_complexity/space_complexity.zig"
 | 
			
		||||
        // Run Command: zig build run_space_complexity
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "space_complexity", 
 | 
			
		||||
            .path = "chapter_computational_complexity/space_complexity.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_space_complexity -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "space_complexity", .path = "chapter_computational_complexity/space_complexity.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_array_and_linkedlist/array.zig"
 | 
			
		||||
        // Run Command: zig build run_array
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "array", 
 | 
			
		||||
            .path = "chapter_array_and_linkedlist/array.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_array -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "array", .path = "chapter_array_and_linkedlist/array.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_array_and_linkedlist/linked_list.zig"
 | 
			
		||||
        // Run Command: zig build run_linked_list
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "linked_list", 
 | 
			
		||||
            .path = "chapter_array_and_linkedlist/linked_list.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_linked_list -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "linked_list", .path = "chapter_array_and_linkedlist/linked_list.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_array_and_linkedlist/list.zig"
 | 
			
		||||
        // Run Command: zig build run_list
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "list", 
 | 
			
		||||
            .path = "chapter_array_and_linkedlist/list.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_list -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "list", .path = "chapter_array_and_linkedlist/list.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_array_and_linkedlist/my_list.zig"
 | 
			
		||||
        // Run Command: zig build run_my_list
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "my_list", 
 | 
			
		||||
            .path = "chapter_array_and_linkedlist/my_list.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_my_list -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "my_list", .path = "chapter_array_and_linkedlist/my_list.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_stack_and_queue/stack.zig"
 | 
			
		||||
        // Run Command: zig build run_stack
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "stack", 
 | 
			
		||||
            .path = "chapter_stack_and_queue/stack.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_stack -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "stack", .path = "chapter_stack_and_queue/stack.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_stack_and_queue/linkedlist_stack.zig"
 | 
			
		||||
        // Run Command: zig build run_linkedlist_stack
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "linkedlist_stack", 
 | 
			
		||||
            .path = "chapter_stack_and_queue/linkedlist_stack.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_linkedlist_stack -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "linkedlist_stack", .path = "chapter_stack_and_queue/linkedlist_stack.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_stack_and_queue/array_stack.zig"
 | 
			
		||||
        // Run Command: zig build run_array_stack
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "array_stack", 
 | 
			
		||||
            .path = "chapter_stack_and_queue/array_stack.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_array_stack -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "array_stack", .path = "chapter_stack_and_queue/array_stack.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_stack_and_queue/queue.zig"
 | 
			
		||||
        // Run Command: zig build run_queue
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "queue", 
 | 
			
		||||
            .path = "chapter_stack_and_queue/queue.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_queue -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "queue", .path = "chapter_stack_and_queue/queue.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_stack_and_queue/array_queue.zig"
 | 
			
		||||
        // Run Command: zig build run_array_queue
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "array_queue", 
 | 
			
		||||
            .path = "chapter_stack_and_queue/array_queue.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_array_queue -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "array_queue", .path = "chapter_stack_and_queue/array_queue.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_stack_and_queue/linkedlist_queue.zig"
 | 
			
		||||
        // Run Command: zig build run_linkedlist_queue
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "linkedlist_queue", 
 | 
			
		||||
            .path = "chapter_stack_and_queue/linkedlist_queue.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_linkedlist_queue -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "linkedlist_queue", .path = "chapter_stack_and_queue/linkedlist_queue.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_stack_and_queue/deque.zig"
 | 
			
		||||
        // Run Command: zig build run_deque
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "deque", 
 | 
			
		||||
            .path = "chapter_stack_and_queue/deque.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_deque -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "deque", .path = "chapter_stack_and_queue/deque.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_stack_and_queue/linkedlist_deque.zig"
 | 
			
		||||
        // Run Command: zig build run_linkedlist_deque
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "linkedlist_deque", 
 | 
			
		||||
            .path = "chapter_stack_and_queue/linkedlist_deque.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_linkedlist_deque -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "linkedlist_deque", .path = "chapter_stack_and_queue/linkedlist_deque.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_hashing/hash_map.zig"
 | 
			
		||||
        // Run Command: zig build run_hash_map
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "hash_map", 
 | 
			
		||||
            .path = "chapter_hashing/hash_map.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_hash_map -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "hash_map", .path = "chapter_hashing/hash_map.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_hashing/array_hash_map.zig"
 | 
			
		||||
        // Run Command: zig build run_array_hash_map
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "array_hash_map", 
 | 
			
		||||
            .path = "chapter_hashing/array_hash_map.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_array_hash_map -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "array_hash_map", .path = "chapter_hashing/array_hash_map.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_tree/binary_tree.zig"
 | 
			
		||||
        // Run Command: zig build run_binary_tree
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "binary_tree", 
 | 
			
		||||
            .path = "chapter_tree/binary_tree.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_binary_tree -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "binary_tree", .path = "chapter_tree/binary_tree.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_tree/binary_tree_bfs.zig"
 | 
			
		||||
        // Run Command: zig build run_binary_tree_bfs
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "binary_tree_bfs", 
 | 
			
		||||
            .path = "chapter_tree/binary_tree_bfs.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_binary_tree_bfs -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "binary_tree_bfs", .path = "chapter_tree/binary_tree_bfs.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_tree/binary_tree_dfs.zig"
 | 
			
		||||
        // Run Command: zig build run_binary_tree_dfs
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "binary_tree_dfs", 
 | 
			
		||||
            .path = "chapter_tree/binary_tree_dfs.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_binary_tree_dfs -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "binary_tree_dfs", .path = "chapter_tree/binary_tree_dfs.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_tree/binary_search_tree.zig"
 | 
			
		||||
        // Run Command: zig build run_binary_search_tree
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "binary_search_tree", 
 | 
			
		||||
            .path = "chapter_tree/binary_search_tree.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_binary_search_tree -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "binary_search_tree", .path = "chapter_tree/binary_search_tree.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_tree/avl_tree.zig"
 | 
			
		||||
        // Run Command: zig build run_avl_tree
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "avl_tree", 
 | 
			
		||||
            .path = "chapter_tree/avl_tree.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_avl_tree -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "avl_tree", .path = "chapter_tree/avl_tree.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_heap/heap.zig"
 | 
			
		||||
        // Run Command: zig build run_heap
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "heap", 
 | 
			
		||||
            .path = "chapter_heap/heap.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_heap -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "heap", .path = "chapter_heap/heap.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_heap/my_heap.zig"
 | 
			
		||||
        // Run Command: zig build run_my_heap
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "my_heap", 
 | 
			
		||||
            .path = "chapter_heap/my_heap.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_my_heap -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "my_heap", .path = "chapter_heap/my_heap.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_searching/linear_search.zig"
 | 
			
		||||
        // Run Command: zig build run_linear_search
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "linear_search", 
 | 
			
		||||
            .path = "chapter_searching/linear_search.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_linear_search -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "linear_search", .path = "chapter_searching/linear_search.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_searching/binary_search.zig"
 | 
			
		||||
        // Run Command: zig build run_binary_search
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "binary_search", 
 | 
			
		||||
            .path = "chapter_searching/binary_search.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_binary_search -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "binary_search", .path = "chapter_searching/binary_search.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_searching/hashing_search.zig"
 | 
			
		||||
        // Run Command: zig build run_hashing_search
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "hashing_search", 
 | 
			
		||||
            .path = "chapter_searching/hashing_search.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_hashing_search -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "hashing_search", .path = "chapter_searching/hashing_search.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_searching/two_sum.zig"
 | 
			
		||||
        // Run Command: zig build run_two_sum
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "two_sum", 
 | 
			
		||||
            .path = "chapter_searching/two_sum.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_two_sum -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "two_sum", .path = "chapter_searching/two_sum.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_sorting/bubble_sort.zig"
 | 
			
		||||
        // Run Command: zig build run_bubble_sort
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "bubble_sort", 
 | 
			
		||||
            .path = "chapter_sorting/bubble_sort.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_bubble_sort -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "bubble_sort", .path = "chapter_sorting/bubble_sort.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_sorting/insertion_sort.zig"
 | 
			
		||||
        // Run Command: zig build run_insertion_sort
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "insertion_sort", 
 | 
			
		||||
            .path = "chapter_sorting/insertion_sort.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_insertion_sort -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "insertion_sort", .path = "chapter_sorting/insertion_sort.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_sorting/quick_sort.zig"
 | 
			
		||||
        // Run Command: zig build run_quick_sort
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "quick_sort", 
 | 
			
		||||
            .path = "chapter_sorting/quick_sort.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_quick_sort -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "quick_sort", .path = "chapter_sorting/quick_sort.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_sorting/merge_sort.zig"
 | 
			
		||||
        // Run Command: zig build run_merge_sort
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "merge_sort", 
 | 
			
		||||
            .path = "chapter_sorting/merge_sort.zig"
 | 
			
		||||
        },
 | 
			
		||||
        // Run Command: zig build run_merge_sort -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "merge_sort", .path = "chapter_sorting/merge_sort.zig" },
 | 
			
		||||
 | 
			
		||||
        // Source File: "chapter_sorting/radix_sort.zig"
 | 
			
		||||
        // Run Command: zig build run_radix_sort
 | 
			
		||||
        .{ 
 | 
			
		||||
            .name = "radix_sort", 
 | 
			
		||||
            .path = "chapter_sorting/radix_sort.zig"
 | 
			
		||||
        },
 | 
			
		||||
        
 | 
			
		||||
        // Run Command: zig build run_radix_sort -Doptimize=ReleaseFast
 | 
			
		||||
        .{ .name = "radix_sort", .path = "chapter_sorting/radix_sort.zig" },
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    inline for (group_name_path) |name_path| {
 | 
			
		||||
@ -250,10 +151,10 @@ pub fn build(b: *std.Build) void {
 | 
			
		||||
            .root_source_file = .{ .path = name_path.path },
 | 
			
		||||
            .target = target,
 | 
			
		||||
            .optimize = optimize,
 | 
			
		||||
             });
 | 
			
		||||
        });
 | 
			
		||||
        exe.addModule("include", b.addModule("", .{
 | 
			
		||||
            .source_file = .{ .path = "include/include.zig" },
 | 
			
		||||
            }));
 | 
			
		||||
        }));
 | 
			
		||||
        b.installArtifact(exe);
 | 
			
		||||
        const run_cmd = b.addRunArtifact(exe);
 | 
			
		||||
        run_cmd.step.dependOn(b.getInstallStep());
 | 
			
		||||
 | 
			
		||||
@ -63,7 +63,7 @@ pub fn traverse(nums: []i32) void {
 | 
			
		||||
// 在数组中查找指定元素
 | 
			
		||||
pub fn find(nums: []i32, target: i32) i32 {
 | 
			
		||||
    for (nums, 0..) |num, i| {
 | 
			
		||||
        if (num == target) return @intCast(i32, i);
 | 
			
		||||
        if (num == target) return @intCast(i);
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -54,7 +54,7 @@ fn quadratic(n: i32) i32 {
 | 
			
		||||
fn bubbleSort(nums: []i32) i32 {
 | 
			
		||||
    var count: i32 = 0;  // 计数器 
 | 
			
		||||
    // 外循环:未排序区间为 [0, i]
 | 
			
		||||
    var i: i32 = @intCast(i32, nums.len ) - 1;
 | 
			
		||||
    var i: i32 = @as(i32, @intCast(nums.len)) - 1;
 | 
			
		||||
    while (i > 0) : (i -= 1) {
 | 
			
		||||
        var j: usize = 0;
 | 
			
		||||
        // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 
 | 
			
		||||
@ -154,7 +154,7 @@ pub fn main() !void {
 | 
			
		||||
    count = quadratic(n);
 | 
			
		||||
    std.debug.print("平方阶的计算操作数量 = {}\n", .{count});
 | 
			
		||||
    for (&nums, 0..) |*num, i| {
 | 
			
		||||
        num.* = n - @intCast(i32, i);  // [n,n-1,...,2,1]
 | 
			
		||||
        num.* = n - @as(i32, @intCast(i));  // [n,n-1,...,2,1]
 | 
			
		||||
    }
 | 
			
		||||
    count = bubbleSort(&nums);
 | 
			
		||||
    std.debug.print("平方阶(冒泡排序)的计算操作数量 = {}\n", .{count});
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@ pub fn randomNumbers(comptime n: usize) [n]i32 {
 | 
			
		||||
    var nums: [n]i32 = undefined;
 | 
			
		||||
    // 生成数组 nums = { 1, 2, 3, ..., n }
 | 
			
		||||
    for (&nums, 0..) |*num, i| {
 | 
			
		||||
        num.* = @intCast(i32, i) + 1;
 | 
			
		||||
        num.* = @as(i32, @intCast(i)) + 1;
 | 
			
		||||
    }
 | 
			
		||||
    // 随机打乱数组元素
 | 
			
		||||
    const rand = std.crypto.random;
 | 
			
		||||
@ -23,7 +23,7 @@ pub fn findOne(nums: []i32) i32 {
 | 
			
		||||
    for (nums, 0..) |num, i| {
 | 
			
		||||
        // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
 | 
			
		||||
        // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
 | 
			
		||||
        if (num == 1) return @intCast(i32, i);
 | 
			
		||||
        if (num == 1) return @intCast(i);
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -18,7 +18,7 @@ fn binarySearch(comptime T: type, nums: std.ArrayList(T), target: T) T {
 | 
			
		||||
        } else if (nums.items[m] > target) {    // 此情况说明 target 在区间 [i, m-1] 中
 | 
			
		||||
            j = m - 1;
 | 
			
		||||
        } else {                                // 找到目标元素,返回其索引
 | 
			
		||||
            return @intCast(T, m);
 | 
			
		||||
            return @intCast(m);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 未找到目标元素,返回 -1
 | 
			
		||||
@ -38,7 +38,7 @@ fn binarySearchLCRO(comptime T: type, nums: std.ArrayList(T), target: T) T {
 | 
			
		||||
        } else if (nums.items[m] > target) {    // 此情况说明 target 在区间 [i, m) 中
 | 
			
		||||
            j = m;
 | 
			
		||||
        } else {                                // 找到目标元素,返回其索引
 | 
			
		||||
            return @intCast(T, m);
 | 
			
		||||
            return @intCast(m);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 未找到目标元素,返回 -1
 | 
			
		||||
 | 
			
		||||
@ -31,7 +31,7 @@ pub fn main() !void {
 | 
			
		||||
    var map = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);
 | 
			
		||||
    defer map.deinit();
 | 
			
		||||
    for (nums, 0..) |num, i| {
 | 
			
		||||
        try map.put(num, @intCast(i32, i));    // key: 元素,value: 索引
 | 
			
		||||
        try map.put(num, @as(i32, @intCast(i)));    // key: 元素,value: 索引
 | 
			
		||||
    }
 | 
			
		||||
    var index = hashingSearchArray(i32, map, target);
 | 
			
		||||
    std.debug.print("目标元素 3 的索引 = {}\n", .{index});
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,7 @@ fn linearSearchArray(comptime T: type, nums: std.ArrayList(T), target: T) T {
 | 
			
		||||
    for (nums.items, 0..) |num, i| {
 | 
			
		||||
        // 找到目标元素, 返回其索引
 | 
			
		||||
        if (num == target) {
 | 
			
		||||
            return @intCast(T, i);
 | 
			
		||||
            return @intCast(i);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 未找到目标元素,返回 -1
 | 
			
		||||
 | 
			
		||||
@ -14,7 +14,7 @@ pub fn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {
 | 
			
		||||
        var j = i + 1;
 | 
			
		||||
        while (j < size) : (j += 1) {
 | 
			
		||||
            if (nums[i] + nums[j] == target) {
 | 
			
		||||
                return [_]i32{@intCast(i32, i), @intCast(i32, j)};
 | 
			
		||||
                return [_]i32{@intCast(i), @intCast(j)};
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@ -31,9 +31,9 @@ pub fn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {
 | 
			
		||||
    // 单层循环,时间复杂度 O(n)
 | 
			
		||||
    while (i < size) : (i += 1) {
 | 
			
		||||
        if (dic.contains(target - nums[i])) {
 | 
			
		||||
            return [_]i32{dic.get(target - nums[i]).?, @intCast(i32, i)};
 | 
			
		||||
            return [_]i32{dic.get(target - nums[i]).?, @intCast(i)};
 | 
			
		||||
        }
 | 
			
		||||
        try dic.put(nums[i], @intCast(i32, i));
 | 
			
		||||
        try dic.put(nums[i], @intCast(i));
 | 
			
		||||
    }
 | 
			
		||||
    return null;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -22,7 +22,7 @@ fn countingSortDigit(nums: []i32, exp: i32) !void {
 | 
			
		||||
    var n = nums.len;
 | 
			
		||||
    // 统计 0~9 各数字的出现次数
 | 
			
		||||
    for (nums) |num| {
 | 
			
		||||
        var d = @bitCast(u32, digit(num, exp)); // 获取 nums[i] 第 k 位,记为 d
 | 
			
		||||
        var d: u32 = @bitCast(digit(num, exp)); // 获取 nums[i] 第 k 位,记为 d
 | 
			
		||||
        counter[d] += 1; // 统计数字 d 的出现次数
 | 
			
		||||
    }
 | 
			
		||||
    // 求前缀和,将“出现个数”转换为“数组索引”
 | 
			
		||||
@ -34,7 +34,7 @@ fn countingSortDigit(nums: []i32, exp: i32) !void {
 | 
			
		||||
    var res = try mem_allocator.alloc(i32, n);
 | 
			
		||||
    i = n - 1;
 | 
			
		||||
    while (i >= 0) : (i -= 1) {
 | 
			
		||||
        var d = @bitCast(u32, digit(nums[i], exp));
 | 
			
		||||
        var d: u32 = @bitCast(digit(nums[i], exp));
 | 
			
		||||
        var j = counter[d] - 1; // 获取 d 在数组中的索引 j
 | 
			
		||||
        res[j] = nums[i];       // 将当前元素填入索引 j
 | 
			
		||||
        counter[d] -= 1;        // 将 d 的数量减 1
 | 
			
		||||
 | 
			
		||||
@ -38,7 +38,7 @@ pub fn AVLTree(comptime T: type) type {
 | 
			
		||||
        // 更新节点高度
 | 
			
		||||
        fn updateHeight(self: *Self, node: ?*inc.TreeNode(T)) void {
 | 
			
		||||
            // 节点高度等于最高子树高度 + 1
 | 
			
		||||
            node.?.height = std.math.max(self.height(node.?.left), self.height(node.?.right)) + 1;
 | 
			
		||||
            node.?.height = @max(self.height(node.?.left), self.height(node.?.right)) + 1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // 获取平衡因子
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user