mirror of
https://github.com/krahets/hello-algo.git
synced 2025-10-31 02:17:06 +08:00
Bug fixes and improvements (#1732)
* Bug fixes * Sync zh and zh-hant versions. * "入列列" -> "入佇列" * Fix hello_algo_mindmap.png
This commit is contained in:
@ -31,7 +31,7 @@ function xorHash(key) {
|
||||
for (const c of key) {
|
||||
hash ^= c.charCodeAt(0);
|
||||
}
|
||||
return hash & MODULUS;
|
||||
return hash % MODULUS;
|
||||
}
|
||||
|
||||
/* 旋轉雜湊 */
|
||||
|
||||
@ -9,9 +9,7 @@ def counting_sort_naive(nums: list[int]):
|
||||
"""計數排序"""
|
||||
# 簡單實現,無法用於排序物件
|
||||
# 1. 統計陣列最大元素 m
|
||||
m = 0
|
||||
for num in nums:
|
||||
m = max(m, num)
|
||||
m = max(nums)
|
||||
# 2. 統計各數字的出現次數
|
||||
# counter[num] 代表 num 的出現次數
|
||||
counter = [0] * (m + 1)
|
||||
|
||||
@ -19,8 +19,7 @@ struct MyList {
|
||||
impl MyList {
|
||||
/* 建構子 */
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
let mut vec = Vec::new();
|
||||
vec.resize(capacity, 0);
|
||||
let mut vec = vec![0; capacity];
|
||||
Self {
|
||||
arr: vec,
|
||||
capacity,
|
||||
@ -92,7 +91,7 @@ impl MyList {
|
||||
};
|
||||
let num = self.arr[index];
|
||||
// 將將索引 index 之後的元素都向前移動一位
|
||||
for j in (index..self.size - 1) {
|
||||
for j in index..self.size - 1 {
|
||||
self.arr[j] = self.arr[j + 1];
|
||||
}
|
||||
// 更新元素數量
|
||||
@ -111,7 +110,7 @@ impl MyList {
|
||||
}
|
||||
|
||||
/* 將串列轉換為陣列 */
|
||||
pub fn to_array(&mut self) -> Vec<i32> {
|
||||
pub fn to_array(&self) -> Vec<i32> {
|
||||
// 僅轉換有效長度範圍內的串列元素
|
||||
let mut arr = Vec::new();
|
||||
for i in 0..self.size {
|
||||
|
||||
@ -13,10 +13,10 @@ struct Pair {
|
||||
|
||||
/* 鏈式位址雜湊表 */
|
||||
struct HashMapChaining {
|
||||
size: i32,
|
||||
capacity: i32,
|
||||
size: usize,
|
||||
capacity: usize,
|
||||
load_thres: f32,
|
||||
extend_ratio: i32,
|
||||
extend_ratio: usize,
|
||||
buckets: Vec<Vec<Pair>>,
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ impl HashMapChaining {
|
||||
|
||||
/* 雜湊函式 */
|
||||
fn hash_func(&self, key: i32) -> usize {
|
||||
key as usize % self.capacity as usize
|
||||
key as usize % self.capacity
|
||||
}
|
||||
|
||||
/* 負載因子 */
|
||||
@ -45,12 +45,11 @@ impl HashMapChaining {
|
||||
/* 刪除操作 */
|
||||
fn remove(&mut self, key: i32) -> Option<String> {
|
||||
let index = self.hash_func(key);
|
||||
let bucket = &mut self.buckets[index];
|
||||
|
||||
// 走訪桶,從中刪除鍵值對
|
||||
for i in 0..bucket.len() {
|
||||
if bucket[i].key == key {
|
||||
let pair = bucket.remove(i);
|
||||
for (i, p) in self.buckets[index].iter_mut().enumerate() {
|
||||
if p.key == key {
|
||||
let pair = self.buckets[index].remove(i);
|
||||
self.size -= 1;
|
||||
return Some(pair.val);
|
||||
}
|
||||
@ -63,7 +62,7 @@ impl HashMapChaining {
|
||||
/* 擴容雜湊表 */
|
||||
fn extend(&mut self) {
|
||||
// 暫存原雜湊表
|
||||
let buckets_tmp = std::mem::replace(&mut self.buckets, vec![]);
|
||||
let buckets_tmp = std::mem::take(&mut self.buckets);
|
||||
|
||||
// 初始化擴容後的新雜湊表
|
||||
self.capacity *= self.extend_ratio;
|
||||
@ -97,30 +96,27 @@ impl HashMapChaining {
|
||||
}
|
||||
|
||||
let index = self.hash_func(key);
|
||||
let bucket = &mut self.buckets[index];
|
||||
|
||||
// 走訪桶,若遇到指定 key ,則更新對應 val 並返回
|
||||
for pair in bucket {
|
||||
for pair in self.buckets[index].iter_mut() {
|
||||
if pair.key == key {
|
||||
pair.val = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
let bucket = &mut self.buckets[index];
|
||||
|
||||
// 若無該 key ,則將鍵值對新增至尾部
|
||||
let pair = Pair { key, val };
|
||||
bucket.push(pair);
|
||||
self.buckets[index].push(pair);
|
||||
self.size += 1;
|
||||
}
|
||||
|
||||
/* 查詢操作 */
|
||||
fn get(&self, key: i32) -> Option<&str> {
|
||||
let index = self.hash_func(key);
|
||||
let bucket = &self.buckets[index];
|
||||
|
||||
// 走訪桶,若找到 key ,則返回對應 val
|
||||
for pair in bucket {
|
||||
for pair in self.buckets[index].iter() {
|
||||
if pair.key == key {
|
||||
return Some(&pair.val);
|
||||
}
|
||||
|
||||
@ -120,7 +120,7 @@ impl<T: Copy> LinkedListDeque<T> {
|
||||
}
|
||||
}
|
||||
self.que_size -= 1; // 更新佇列長度
|
||||
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
|
||||
old_front.borrow().val
|
||||
})
|
||||
}
|
||||
// 佇列尾出列操作
|
||||
@ -136,7 +136,7 @@ impl<T: Copy> LinkedListDeque<T> {
|
||||
}
|
||||
}
|
||||
self.que_size -= 1; // 更新佇列長度
|
||||
Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val
|
||||
old_rear.borrow().val
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -163,12 +163,16 @@ impl<T: Copy> LinkedListDeque<T> {
|
||||
|
||||
/* 返回陣列用於列印 */
|
||||
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
||||
if let Some(node) = head {
|
||||
let mut nums = self.to_array(node.borrow().next.as_ref());
|
||||
nums.insert(0, node.borrow().val);
|
||||
return nums;
|
||||
let mut res: Vec<T> = Vec::new();
|
||||
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
|
||||
if let Some(cur) = cur {
|
||||
res.push(cur.borrow().val);
|
||||
recur(cur.borrow().next.as_ref(), res);
|
||||
}
|
||||
}
|
||||
return Vec::new();
|
||||
|
||||
recur(head, &mut res);
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -67,7 +67,7 @@ impl<T: Copy> LinkedListQueue<T> {
|
||||
}
|
||||
}
|
||||
self.que_size -= 1;
|
||||
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
|
||||
old_front.borrow().val
|
||||
})
|
||||
}
|
||||
|
||||
@ -78,12 +78,18 @@ impl<T: Copy> LinkedListQueue<T> {
|
||||
|
||||
/* 將鏈結串列轉化為 Array 並返回 */
|
||||
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
||||
if let Some(node) = head {
|
||||
let mut nums = self.to_array(node.borrow().next.as_ref());
|
||||
nums.insert(0, node.borrow().val);
|
||||
return nums;
|
||||
let mut res: Vec<T> = Vec::new();
|
||||
|
||||
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
|
||||
if let Some(cur) = cur {
|
||||
res.push(cur.borrow().val);
|
||||
recur(cur.borrow().next.as_ref(), res);
|
||||
}
|
||||
}
|
||||
return Vec::new();
|
||||
|
||||
recur(head, &mut res);
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -45,16 +45,10 @@ impl<T: Copy> LinkedListStack<T> {
|
||||
/* 出堆疊 */
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
self.stack_peek.take().map(|old_head| {
|
||||
match old_head.borrow_mut().next.take() {
|
||||
Some(new_head) => {
|
||||
self.stack_peek = Some(new_head);
|
||||
}
|
||||
None => {
|
||||
self.stack_peek = None;
|
||||
}
|
||||
}
|
||||
self.stack_peek = old_head.borrow_mut().next.take();
|
||||
self.stk_size -= 1;
|
||||
Rc::try_unwrap(old_head).ok().unwrap().into_inner().val
|
||||
|
||||
old_head.borrow().val
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -31,7 +31,7 @@ function xorHash(key: string): number {
|
||||
for (const c of key) {
|
||||
hash ^= c.charCodeAt(0);
|
||||
}
|
||||
return hash & MODULUS;
|
||||
return hash % MODULUS;
|
||||
}
|
||||
|
||||
/* 旋轉雜湊 */
|
||||
|
||||
Reference in New Issue
Block a user