mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-12 19:01:42 +08:00
Bug fixes and improvements (#1572)
* Sync zh and zh-hant versions. * Remove the polyfill.io link from mkdocs.yml * Update contributors' info for code reviewers and en/zh-hant versions reviewers. * Fix graph.md * Update avatars for English version reviewers. * Sync zh and zh-hant versions. * Fix two_sum_brute_force.png * Sync zh and zh-hant versions. Optimize structrue of index.html. * Format index.html
This commit is contained in:
57
zh-hant/codes/rust/src/include/list_node.rs
Normal file
57
zh-hant/codes/rust/src/include/list_node.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* File: list_node.rs
|
||||
* Created Time: 2023-03-05
|
||||
* Author: codingonion (coderonion@gmail.com), rongyi (hiarongyi@gmail.com)
|
||||
*/
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ListNode<T> {
|
||||
pub val: T,
|
||||
pub next: Option<Rc<RefCell<ListNode<T>>>>,
|
||||
}
|
||||
|
||||
impl<T> ListNode<T> {
|
||||
pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {
|
||||
Rc::new(RefCell::new(ListNode { val, next: None }))
|
||||
}
|
||||
|
||||
/* 將陣列反序列化為鏈結串列 */
|
||||
pub fn arr_to_linked_list(array: &[T]) -> Option<Rc<RefCell<ListNode<T>>>>
|
||||
where
|
||||
T: Copy + Clone,
|
||||
{
|
||||
let mut head = None;
|
||||
// insert in reverse order
|
||||
for item in array.iter().rev() {
|
||||
let node = Rc::new(RefCell::new(ListNode {
|
||||
val: *item,
|
||||
next: head.take(),
|
||||
}));
|
||||
head = Some(node);
|
||||
}
|
||||
head
|
||||
}
|
||||
|
||||
/* 將鏈結串列轉化為雜湊表 */
|
||||
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();
|
||||
let mut node = linked_list;
|
||||
|
||||
while let Some(cur) = node {
|
||||
let borrow = cur.borrow();
|
||||
hashmap.insert(borrow.val.clone(), cur.clone());
|
||||
node = borrow.next.clone();
|
||||
}
|
||||
|
||||
hashmap
|
||||
}
|
||||
}
|
||||
16
zh-hant/codes/rust/src/include/mod.rs
Normal file
16
zh-hant/codes/rust/src/include/mod.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* File: include.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: codingonion (coderonion@gmail.com), xBLACKICEx (xBLACKICE@outlook.com)
|
||||
*/
|
||||
|
||||
pub mod list_node;
|
||||
pub mod print_util;
|
||||
pub mod tree_node;
|
||||
pub mod vertex;
|
||||
|
||||
// rexport to include
|
||||
pub use list_node::*;
|
||||
pub use print_util::*;
|
||||
pub use tree_node::*;
|
||||
pub use vertex::*;
|
||||
103
zh-hant/codes/rust/src/include/print_util.rs
Normal file
103
zh-hant/codes/rust/src/include/print_util.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* File: print_util.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: codingonion (coderonion@gmail.com), xBLACKICEx (xBLACKICEx@outlook.com)
|
||||
*/
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::fmt::Display;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::list_node::ListNode;
|
||||
use super::tree_node::{TreeNode, vec_to_tree};
|
||||
|
||||
struct Trunk<'a, 'b> {
|
||||
prev: Option<&'a Trunk<'a, 'b>>,
|
||||
str: Cell<&'b str>,
|
||||
}
|
||||
|
||||
/* 列印陣列 */
|
||||
pub fn print_array<T: Display>(nums: &[T]) {
|
||||
print!("[");
|
||||
if nums.len() > 0 {
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
print!("{}{}", num, if i == nums.len() - 1 {"]"} else {", "} );
|
||||
}
|
||||
} else {
|
||||
print!("]");
|
||||
}
|
||||
}
|
||||
|
||||
/* 列印雜湊表 */
|
||||
pub fn print_hash_map<TKey: Display, TValue: Display>(map: &HashMap<TKey, TValue>) {
|
||||
for (key, value) in map {
|
||||
println!("{key} -> {value}");
|
||||
}
|
||||
}
|
||||
|
||||
/* 列印佇列(雙向佇列) */
|
||||
pub fn print_queue<T: Display>(queue: &VecDeque<T>) {
|
||||
print!("[");
|
||||
let iter = queue.iter();
|
||||
for (i, data) in iter.enumerate() {
|
||||
print!("{}{}", data, if i == queue.len() - 1 {"]"} else {", "} );
|
||||
}
|
||||
}
|
||||
|
||||
/* 列印鏈結串列 */
|
||||
pub fn print_linked_list<T: Display>(head: &Rc<RefCell<ListNode<T>>>) {
|
||||
print!("{}{}", head.borrow().val, if head.borrow().next.is_none() {"\n"} else {" -> "});
|
||||
if let Some(node) = &head.borrow().next {
|
||||
return print_linked_list(node);
|
||||
}
|
||||
}
|
||||
|
||||
/* 列印二元樹 */
|
||||
pub fn print_tree(root: &Rc<RefCell<TreeNode>>) {
|
||||
_print_tree(Some(root), None, false);
|
||||
}
|
||||
|
||||
/* 列印二元樹 */
|
||||
fn _print_tree(root: Option<&Rc<RefCell<TreeNode>>>, prev: Option<&Trunk>, is_right: bool) {
|
||||
if let Some(node) = root {
|
||||
let mut prev_str = " ";
|
||||
let trunk = Trunk { prev, str: Cell::new(prev_str) };
|
||||
_print_tree(node.borrow().right.as_ref(), Some(&trunk), true);
|
||||
|
||||
if prev.is_none() {
|
||||
trunk.str.set("———");
|
||||
} else if is_right {
|
||||
trunk.str.set("/———");
|
||||
prev_str = " |";
|
||||
} else {
|
||||
trunk.str.set("\\———");
|
||||
prev.as_ref().unwrap().str.set(prev_str);
|
||||
}
|
||||
|
||||
show_trunks(Some(&trunk));
|
||||
println!(" {}", node.borrow().val);
|
||||
if let Some(prev) = prev {
|
||||
prev.str.set(prev_str);
|
||||
}
|
||||
trunk.str.set(" |");
|
||||
|
||||
_print_tree(node.borrow().left.as_ref(), Some(&trunk), false);
|
||||
}
|
||||
}
|
||||
|
||||
fn show_trunks(trunk: Option<&Trunk>) {
|
||||
if let Some(trunk) = trunk {
|
||||
show_trunks(trunk.prev);
|
||||
print!("{}", trunk.str.get());
|
||||
}
|
||||
}
|
||||
|
||||
/* 列印堆積 */
|
||||
pub fn print_heap(heap: Vec<i32>) {
|
||||
println!("堆積的陣列表示:{:?}", heap);
|
||||
println!("堆積的樹狀表示:");
|
||||
if let Some(root) = vec_to_tree(heap.into_iter().map(|val| Some(val)).collect()) {
|
||||
print_tree(&root);
|
||||
}
|
||||
}
|
||||
92
zh-hant/codes/rust/src/include/tree_node.rs
Normal file
92
zh-hant/codes/rust/src/include/tree_node.rs
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* File: tree_node.rs
|
||||
* Created Time: 2023-02-27
|
||||
* Author: xBLACKICEx (xBLACKICE@outlook.com), night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/* 二元樹節點型別 */
|
||||
#[derive(Debug)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub height: i32,
|
||||
pub parent: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
/* 建構子 */
|
||||
pub fn new(val: i32) -> Rc<RefCell<Self>> {
|
||||
Rc::new(RefCell::new(Self {
|
||||
val,
|
||||
height: 0,
|
||||
parent: None,
|
||||
left: None,
|
||||
right: None,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! op_vec {
|
||||
( $( $x:expr ),* ) => {
|
||||
vec![
|
||||
$( Option::from($x).map(|x| x) ),*
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
// 序列化編碼規則請參考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
// 二元樹的陣列表示:
|
||||
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
// 二元樹的鏈結串列表示:
|
||||
// /——— 15
|
||||
// /——— 7
|
||||
// /——— 3
|
||||
// | \——— 6
|
||||
// | \——— 12
|
||||
// ——— 1
|
||||
// \——— 2
|
||||
// | /——— 9
|
||||
// \——— 4
|
||||
// \——— 8
|
||||
|
||||
/* 將串列反序列化為二元樹:遞迴 */
|
||||
fn vec_to_tree_dfs(arr: &[Option<i32>], i: usize) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
if i >= arr.len() || arr[i].is_none() {
|
||||
return None;
|
||||
}
|
||||
let root = TreeNode::new(arr[i].unwrap());
|
||||
root.borrow_mut().left = vec_to_tree_dfs(arr, 2 * i + 1);
|
||||
root.borrow_mut().right = vec_to_tree_dfs(arr, 2 * i + 2);
|
||||
Some(root)
|
||||
}
|
||||
|
||||
/* 將串列反序列化為二元樹 */
|
||||
pub fn vec_to_tree(arr: Vec<Option<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
vec_to_tree_dfs(&arr, 0)
|
||||
}
|
||||
|
||||
/* 將二元樹序列化為串列:遞迴 */
|
||||
fn tree_to_vec_dfs(root: Option<&Rc<RefCell<TreeNode>>>, i: usize, res: &mut Vec<Option<i32>>) {
|
||||
if let Some(root) = root {
|
||||
// i + 1 is the minimum valid size to access index i
|
||||
while res.len() < i + 1 {
|
||||
res.push(None);
|
||||
}
|
||||
res[i] = Some(root.borrow().val);
|
||||
tree_to_vec_dfs(root.borrow().left.as_ref(), 2 * i + 1, res);
|
||||
tree_to_vec_dfs(root.borrow().right.as_ref(), 2 * i + 2, res);
|
||||
}
|
||||
}
|
||||
|
||||
/* 將二元樹序列化為串列 */
|
||||
pub fn tree_to_vec(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Option<i32>> {
|
||||
let mut res = vec![];
|
||||
tree_to_vec_dfs(root.as_ref(), 0, &mut res);
|
||||
res
|
||||
}
|
||||
21
zh-hant/codes/rust/src/include/vertex.rs
Normal file
21
zh-hant/codes/rust/src/include/vertex.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* File: vertex.rs
|
||||
* Created Time: 2023-07-13
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
/* 頂點型別 */
|
||||
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
|
||||
pub struct Vertex {
|
||||
pub val: i32,
|
||||
}
|
||||
|
||||
/* 輸入值串列 vals ,返回頂點串列 vets */
|
||||
pub fn vals_to_vets(vals: Vec<i32>) -> Vec<Vertex> {
|
||||
vals.into_iter().map(|val| Vertex { val }).collect()
|
||||
}
|
||||
|
||||
/* 輸入頂點串列 vets ,返回值串列 vals */
|
||||
pub fn vets_to_vals(vets: Vec<Vertex>) -> Vec<i32> {
|
||||
vets.into_iter().map(|vet| vet.val).collect()
|
||||
}
|
||||
1
zh-hant/codes/rust/src/lib.rs
Normal file
1
zh-hant/codes/rust/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod include;
|
||||
Reference in New Issue
Block a user