diff --git a/zh-Hant/docs/chapter_array_and_linkedlist/list.md b/zh-Hant/docs/chapter_array_and_linkedlist/list.md index c719d662f..1d1a7a623 100755 --- a/zh-Hant/docs/chapter_array_and_linkedlist/list.md +++ b/zh-Hant/docs/chapter_array_and_linkedlist/list.md @@ -2034,7 +2034,7 @@ comments: true panic!("索引越界") }; let num = self.arr[index]; - // 將將索引 index 之後的元素都向前移動一位 + // 將索引 index 之後的元素都向前移動一位 for j in index..self.size - 1 { self.arr[j] = self.arr[j + 1]; } diff --git a/zh-Hant/docs/chapter_computational_complexity/time_complexity.md b/zh-Hant/docs/chapter_computational_complexity/time_complexity.md index 3b7c704ae..4a291c397 100755 --- a/zh-Hant/docs/chapter_computational_complexity/time_complexity.md +++ b/zh-Hant/docs/chapter_computational_complexity/time_complexity.md @@ -757,7 +757,7 @@ $T(n)$ 是一次函式,說明其執行時間的增長趨勢是線性的,因 若存在正實數 $c$ 和實數 $n_0$ ,使得對於所有的 $n > n_0$ ,均有 $T(n) \leq c \cdot f(n)$ ,則可認為 $f(n)$ 給出了 $T(n)$ 的一個漸近上界,記為 $T(n) = O(f(n))$ 。 -如圖 2-8 所示,計算漸近上界就是尋找一個函式 $f(n)$ ,使得當 $n$ 趨向於無窮大時,$T(n)$ 和 $f(n)$ 處於相同的增長級別,僅相差一個常數項 $c$ 的倍數。 +如圖 2-8 所示,計算漸近上界就是尋找一個函式 $f(n)$ ,使得當 $n$ 趨向於無窮大時,$T(n)$ 和 $f(n)$ 處於相同的增長級別,僅相差一個常數係數 $c$。 ![函式的漸近上界](time_complexity.assets/asymptotic_upper_bound.png){ class="animation-figure" } @@ -771,9 +771,9 @@ $T(n)$ 是一次函式,說明其執行時間的增長趨勢是線性的,因 ### 1.   第一步:統計操作數量 -針對程式碼,逐行從上到下計算即可。然而,由於上述 $c \cdot f(n)$ 中的常數項 $c$ 可以取任意大小,**因此操作數量 $T(n)$ 中的各種係數、常數項都可以忽略**。根據此原則,可以總結出以下計數簡化技巧。 +針對程式碼,逐行從上到下計算即可。然而,由於上述 $c \cdot f(n)$ 中的常數係數 $c$ 可以取任意大小,**因此操作數量 $T(n)$ 中的各種係數、常數項都可以忽略**。根據此原則,可以總結出以下計數簡化技巧。 -1. **忽略 $T(n)$ 中的常數項**。因為它們都與 $n$ 無關,所以對時間複雜度不產生影響。 +1. **忽略 $T(n)$ 中的常數**。因為它們都與 $n$ 無關,所以對時間複雜度不產生影響。 2. **省略所有係數**。例如,迴圈 $2n$ 次、$5n + 1$ 次等,都可以簡化記為 $n$ 次,因為 $n$ 前面的係數對時間複雜度沒有影響。 3. **迴圈巢狀時使用乘法**。總操作數量等於外層迴圈和內層迴圈操作數量之積,每一層迴圈依然可以分別套用第 `1.` 點和第 `2.` 點的技巧。 diff --git a/zh-Hant/docs/chapter_stack_and_queue/deque.md b/zh-Hant/docs/chapter_stack_and_queue/deque.md index d3434bbfd..4b0115f26 100644 --- a/zh-Hant/docs/chapter_stack_and_queue/deque.md +++ b/zh-Hant/docs/chapter_stack_and_queue/deque.md @@ -1609,11 +1609,11 @@ comments: true /* 判斷雙向佇列是否為空 */ pub fn is_empty(&self) -> bool { - return self.size() == 0; + return self.que_size == 0; } /* 入列操作 */ - pub fn push(&mut self, num: T, is_front: bool) { + fn push(&mut self, num: T, is_front: bool) { let node = ListNode::new(num); // 佇列首入列操作 if is_front { @@ -1661,7 +1661,7 @@ comments: true } /* 出列操作 */ - pub fn pop(&mut self, is_front: bool) -> Option { + fn pop(&mut self, is_front: bool) -> Option { // 若佇列為空,直接返回 None if self.is_empty() { return None; @@ -3320,17 +3320,17 @@ comments: true ```rust title="array_deque.rs" /* 基於環形陣列實現的雙向佇列 */ - struct ArrayDeque { - nums: Vec, // 用於儲存雙向佇列元素的陣列 + struct ArrayDeque { + nums: Vec, // 用於儲存雙向佇列元素的陣列 front: usize, // 佇列首指標,指向佇列首元素 que_size: usize, // 雙向佇列長度 } - impl ArrayDeque { + impl ArrayDeque { /* 建構子 */ pub fn new(capacity: usize) -> Self { Self { - nums: vec![0; capacity], + nums: vec![T::default(); capacity], front: 0, que_size: 0, } @@ -3356,11 +3356,11 @@ comments: true // 透過取餘操作實現陣列首尾相連 // 當 i 越過陣列尾部後,回到頭部 // 當 i 越過陣列頭部後,回到尾部 - return ((i + self.capacity() as i32) % self.capacity() as i32) as usize; + ((i + self.capacity() as i32) % self.capacity() as i32) as usize } /* 佇列首入列 */ - pub fn push_first(&mut self, num: i32) { + pub fn push_first(&mut self, num: T) { if self.que_size == self.capacity() { println!("雙向佇列已滿"); return; @@ -3374,7 +3374,7 @@ comments: true } /* 佇列尾入列 */ - pub fn push_last(&mut self, num: i32) { + pub fn push_last(&mut self, num: T) { if self.que_size == self.capacity() { println!("雙向佇列已滿"); return; @@ -3387,7 +3387,7 @@ comments: true } /* 佇列首出列 */ - fn pop_first(&mut self) -> i32 { + fn pop_first(&mut self) -> T { let num = self.peek_first(); // 佇列首指標向後移動一位 self.front = self.index(self.front as i32 + 1); @@ -3396,14 +3396,14 @@ comments: true } /* 佇列尾出列 */ - fn pop_last(&mut self) -> i32 { + fn pop_last(&mut self) -> T { let num = self.peek_last(); self.que_size -= 1; num } /* 訪問佇列首元素 */ - fn peek_first(&self) -> i32 { + fn peek_first(&self) -> T { if self.is_empty() { panic!("雙向佇列為空") }; @@ -3411,7 +3411,7 @@ comments: true } /* 訪問佇列尾元素 */ - fn peek_last(&self) -> i32 { + fn peek_last(&self) -> T { if self.is_empty() { panic!("雙向佇列為空") }; @@ -3421,9 +3421,9 @@ comments: true } /* 返回陣列用於列印 */ - fn to_array(&self) -> Vec { + fn to_array(&self) -> Vec { // 僅轉換有效長度範圍內的串列元素 - let mut res = vec![0; self.que_size]; + let mut res = vec![T::default(); self.que_size]; let mut j = self.front; for i in 0..self.que_size { res[i] = self.nums[self.index(j as i32)]; diff --git a/zh-Hant/docs/chapter_stack_and_queue/queue.md b/zh-Hant/docs/chapter_stack_and_queue/queue.md index 9f57f8b40..dc354d8eb 100755 --- a/zh-Hant/docs/chapter_stack_and_queue/queue.md +++ b/zh-Hant/docs/chapter_stack_and_queue/queue.md @@ -1036,7 +1036,7 @@ comments: true /* 判斷佇列是否為空 */ pub fn is_empty(&self) -> bool { - return self.size() == 0; + return self.que_size == 0; } /* 入列 */ @@ -2082,18 +2082,18 @@ comments: true ```rust title="array_queue.rs" /* 基於環形陣列實現的佇列 */ - struct ArrayQueue { - nums: Vec, // 用於儲存佇列元素的陣列 + struct ArrayQueue { + nums: Vec, // 用於儲存佇列元素的陣列 front: i32, // 佇列首指標,指向佇列首元素 que_size: i32, // 佇列長度 que_capacity: i32, // 佇列容量 } - impl ArrayQueue { + impl ArrayQueue { /* 建構子 */ - fn new(capacity: i32) -> ArrayQueue { + fn new(capacity: i32) -> ArrayQueue { ArrayQueue { - nums: vec![0; capacity as usize], + nums: vec![T::default(); capacity as usize], front: 0, que_size: 0, que_capacity: capacity, @@ -2116,7 +2116,7 @@ comments: true } /* 入列 */ - fn push(&mut self, num: i32) { + fn push(&mut self, num: T) { if self.que_size == self.capacity() { println!("佇列已滿"); return; @@ -2130,7 +2130,7 @@ comments: true } /* 出列 */ - fn pop(&mut self) -> i32 { + fn pop(&mut self) -> T { let num = self.peek(); // 佇列首指標向後移動一位,若越過尾部,則返回到陣列頭部 self.front = (self.front + 1) % self.que_capacity; @@ -2139,7 +2139,7 @@ comments: true } /* 訪問佇列首元素 */ - fn peek(&self) -> i32 { + fn peek(&self) -> T { if self.is_empty() { panic!("index out of bounds"); } @@ -2147,10 +2147,10 @@ comments: true } /* 返回陣列 */ - fn to_vector(&self) -> Vec { + fn to_vector(&self) -> Vec { let cap = self.que_capacity; let mut j = self.front; - let mut arr = vec![0; self.que_size as usize]; + let mut arr = vec![T::default(); cap as usize]; for i in 0..self.que_size { arr[i as usize] = self.nums[(j % cap) as usize]; j += 1; diff --git a/zh-Hant/docs/chapter_stack_and_queue/stack.md b/zh-Hant/docs/chapter_stack_and_queue/stack.md index 37060a27e..b4945b278 100755 --- a/zh-Hant/docs/chapter_stack_and_queue/stack.md +++ b/zh-Hant/docs/chapter_stack_and_queue/stack.md @@ -977,13 +977,17 @@ comments: true } /* 將 List 轉化為 Array 並返回 */ - pub fn to_array(&self, head: Option<&Rc>>>) -> Vec { - if let Some(node) = head { - let mut nums = self.to_array(node.borrow().next.as_ref()); - nums.push(node.borrow().val); - return nums; + pub fn to_array(&self) -> Vec { + fn _to_array(head: Option<&Rc>>>) -> Vec { + if let Some(node) = head { + let mut nums = _to_array(node.borrow().next.as_ref()); + nums.push(node.borrow().val); + return nums; + } + return Vec::new(); } - return Vec::new(); + + _to_array(self.peek()) } } ```