From 0cfb62d8f9d7beff9d2b4503635380cd7bd04ae7 Mon Sep 17 00:00:00 2001 From: krahets Date: Tue, 14 Jan 2025 03:42:26 +0800 Subject: [PATCH] deploy --- chapter_array_and_linkedlist/array/index.html | 20 +++--- .../linked_list/index.html | 68 ++++++++++-------- .../performance_evaluation/index.html | 2 +- chapter_hashing/hash_algorithm/index.html | 2 +- .../ram_and_cache/index.html | 48 ++++++------- .../sorting_algorithm/index.html | 12 ++-- en/search/search_index.json | 2 +- en/sitemap.xml.gz | Bin 1008 -> 1008 bytes search/search_index.json | 2 +- sitemap.xml.gz | Bin 1011 -> 1011 bytes .../array/index.html | 20 +++--- .../linked_list/index.html | 68 ++++++++++-------- .../performance_evaluation/index.html | 2 +- .../chapter_hashing/hash_algorithm/index.html | 2 +- zh-hant/search/search_index.json | 2 +- zh-hant/sitemap.xml.gz | Bin 1010 -> 1010 bytes 16 files changed, 135 insertions(+), 115 deletions(-) diff --git a/chapter_array_and_linkedlist/array/index.html b/chapter_array_and_linkedlist/array/index.html index 6e7ed586a..f21789c23 100644 --- a/chapter_array_and_linkedlist/array/index.html +++ b/chapter_array_and_linkedlist/array/index.html @@ -4591,10 +4591,11 @@ _count += nums[i]; } // 直接遍历数组元素 - for num in nums { - _count += num; - } -} + _count = 0; + for &num in nums { + _count += num; + } +}
@@ -4972,12 +4973,11 @@ // 初始化一个扩展长度后的数组 let mut res: Vec<i32> = vec![0; nums.len() + enlarge]; // 将原数组中的所有元素复制到新 - for i in 0..nums.len() { - res[i] = nums[i]; - } - // 返回扩展后的新数组 - res -} + res[0..nums.len()].copy_from_slice(nums); + + // 返回扩展后的新数组 + res +}
diff --git a/chapter_array_and_linkedlist/linked_list/index.html b/chapter_array_and_linkedlist/linked_list/index.html index 598544279..855a93147 100644 --- a/chapter_array_and_linkedlist/linked_list/index.html +++ b/chapter_array_and_linkedlist/linked_list/index.html @@ -4453,16 +4453,13 @@
linked_list.rs
/* 删除链表的节点 n0 之后的首个节点 */
 #[allow(non_snake_case)]
 pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
-    if n0.borrow().next.is_none() {
-        return;
-    };
-    // n0 -> P -> n1
-    let P = n0.borrow_mut().next.take();
-    if let Some(node) = P {
-        let n1 = node.borrow_mut().next.take();
-        n0.borrow_mut().next = n1;
-    }
-}
+    // n0 -> P -> n1
+    let P = n0.borrow_mut().next.take();
+    if let Some(node) = P {
+        let n1 = node.borrow_mut().next.take();
+        n0.borrow_mut().next = n1;
+    }
+}
 
@@ -4638,16 +4635,24 @@
linked_list.rs
/* 访问链表中索引为 index 的节点 */
-pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {
-    if index <= 0 {
-        return head;
-    };
-    if let Some(node) = &head.borrow().next {
-        return access(node.clone(), index - 1);
-    }
-
-    return head;
-}
+pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> {
+    fn dfs<T>(
+        head: Option<&Rc<RefCell<ListNode<T>>>>,
+        index: i32,
+    ) -> Option<Rc<RefCell<ListNode<T>>>> {
+        if index <= 0 {
+            return head.cloned();
+        }
+
+        if let Some(node) = head {
+            dfs(node.borrow().next.as_ref(), index - 1)
+        } else {
+            None
+        }
+    }
+
+    dfs(Some(head).as_ref(), index)
+}
 
@@ -4843,15 +4848,20 @@
linked_list.rs
/* 在链表中查找值为 target 的首个节点 */
-pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {
-    if head.borrow().val == target {
-        return index;
-    };
-    if let Some(node) = &head.borrow_mut().next {
-        return find(node.clone(), target, index + 1);
-    }
-    return -1;
-}
+pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {
+    fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 {
+        if let Some(node) = head {
+            if node.borrow().val == target {
+                return idx;
+            }
+            return find(node.borrow().next.as_ref(), target, idx + 1);
+        } else {
+            -1
+        }
+    }
+
+    find(Some(head).as_ref(), target, 0)
+}
 
diff --git a/chapter_computational_complexity/performance_evaluation/index.html b/chapter_computational_complexity/performance_evaluation/index.html index dd3b82e7f..5f476b612 100644 --- a/chapter_computational_complexity/performance_evaluation/index.html +++ b/chapter_computational_complexity/performance_evaluation/index.html @@ -3666,7 +3666,7 @@

一方面,难以排除测试环境的干扰因素。硬件配置会影响算法的性能表现。比如一个算法的并行度较高,那么它就更适合在多核 CPU 上运行,一个算法的内存操作密集,那么它在高性能内存上的表现就会更好。也就是说,算法在不同的机器上的测试结果可能是不一致的。这意味着我们需要在各种机器上进行测试,统计平均效率,而这是不现实的。

另一方面,展开完整测试非常耗费资源。随着输入数据量的变化,算法会表现出不同的效率。例如,在输入数据量较小时,算法 A 的运行时间比算法 B 短;而在输入数据量较大时,测试结果可能恰恰相反。因此,为了得到有说服力的结论,我们需要测试各种规模的输入数据,而这需要耗费大量的计算资源。

2.1.2   理论估算

-

由于实际测试具有较大的局限性,因此我们可以考虑仅通过一些计算来评估算法的效率。这种估算方法被称为渐近复杂度分析(asymptotic complexity analysis),简称复杂度分析

+

由于实际测试具有较大的局限性,我们可以考虑仅通过一些计算来评估算法的效率。这种估算方法被称为渐近复杂度分析(asymptotic complexity analysis),简称复杂度分析

复杂度分析能够体现算法运行所需的时间和空间资源与输入数据大小之间的关系。它描述了随着输入数据大小的增加,算法执行所需时间和空间的增长趋势。这个定义有些拗口,我们可以将其分为三个重点来理解。

@@ -4915,12 +4916,11 @@ // 初始化一個擴展長度後的陣列 let mut res: Vec<i32> = vec![0; nums.len() + enlarge]; // 將原陣列中的所有元素複製到新 - for i in 0..nums.len() { - res[i] = nums[i]; - } - // 返回擴展後的新陣列 - res -} + res[0..nums.len()].copy_from_slice(nums); + + // 返回擴展後的新陣列 + res +}
diff --git a/zh-hant/chapter_array_and_linkedlist/linked_list/index.html b/zh-hant/chapter_array_and_linkedlist/linked_list/index.html index 070db9ac5..5398c7a50 100644 --- a/zh-hant/chapter_array_and_linkedlist/linked_list/index.html +++ b/zh-hant/chapter_array_and_linkedlist/linked_list/index.html @@ -4396,16 +4396,13 @@
linked_list.rs
/* 刪除鏈結串列的節點 n0 之後的首個節點 */
 #[allow(non_snake_case)]
 pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
-    if n0.borrow().next.is_none() {
-        return;
-    };
-    // n0 -> P -> n1
-    let P = n0.borrow_mut().next.take();
-    if let Some(node) = P {
-        let n1 = node.borrow_mut().next.take();
-        n0.borrow_mut().next = n1;
-    }
-}
+    // n0 -> P -> n1
+    let P = n0.borrow_mut().next.take();
+    if let Some(node) = P {
+        let n1 = node.borrow_mut().next.take();
+        n0.borrow_mut().next = n1;
+    }
+}
 
@@ -4581,16 +4578,24 @@
linked_list.rs
/* 訪問鏈結串列中索引為 index 的節點 */
-pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {
-    if index <= 0 {
-        return head;
-    };
-    if let Some(node) = &head.borrow().next {
-        return access(node.clone(), index - 1);
-    }
-
-    return head;
-}
+pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> {
+    fn dfs<T>(
+        head: Option<&Rc<RefCell<ListNode<T>>>>,
+        index: i32,
+    ) -> Option<Rc<RefCell<ListNode<T>>>> {
+        if index <= 0 {
+            return head.cloned();
+        }
+
+        if let Some(node) = head {
+            dfs(node.borrow().next.as_ref(), index - 1)
+        } else {
+            None
+        }
+    }
+
+    dfs(Some(head).as_ref(), index)
+}
 
@@ -4786,15 +4791,20 @@
linked_list.rs
/* 在鏈結串列中查詢值為 target 的首個節點 */
-pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {
-    if head.borrow().val == target {
-        return index;
-    };
-    if let Some(node) = &head.borrow_mut().next {
-        return find(node.clone(), target, index + 1);
-    }
-    return -1;
-}
+pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {
+    fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 {
+        if let Some(node) = head {
+            if node.borrow().val == target {
+                return idx;
+            }
+            return find(node.borrow().next.as_ref(), target, idx + 1);
+        } else {
+            -1
+        }
+    }
+
+    find(Some(head).as_ref(), target, 0)
+}
 
diff --git a/zh-hant/chapter_computational_complexity/performance_evaluation/index.html b/zh-hant/chapter_computational_complexity/performance_evaluation/index.html index 33e0047f5..d43a24eb2 100644 --- a/zh-hant/chapter_computational_complexity/performance_evaluation/index.html +++ b/zh-hant/chapter_computational_complexity/performance_evaluation/index.html @@ -3609,7 +3609,7 @@

一方面,難以排除測試環境的干擾因素。硬體配置會影響演算法的效能表現。比如一個演算法的並行度較高,那麼它就更適合在多核 CPU 上執行,一個演算法的記憶體操作密集,那麼它在高效能記憶體上的表現就會更好。也就是說,演算法在不同的機器上的測試結果可能是不一致的。這意味著我們需要在各種機器上進行測試,統計平均效率,而這是不現實的。

另一方面,展開完整測試非常耗費資源。隨著輸入資料量的變化,演算法會表現出不同的效率。例如,在輸入資料量較小時,演算法 A 的執行時間比演算法 B 短;而在輸入資料量較大時,測試結果可能恰恰相反。因此,為了得到有說服力的結論,我們需要測試各種規模的輸入資料,而這需要耗費大量的計算資源。

2.1.2   理論估算

-

由於實際測試具有較大的侷限性,因此我們可以考慮僅透過一些計算來評估演算法的效率。這種估算方法被稱為漸近複雜度分析(asymptotic complexity analysis),簡稱複雜度分析

+

由於實際測試具有較大的侷限性,我們可以考慮僅透過一些計算來評估演算法的效率。這種估算方法被稱為漸近複雜度分析(asymptotic complexity analysis),簡稱複雜度分析

複雜度分析能夠體現演算法執行所需的時間和空間資源與輸入資料大小之間的關係。它描述了隨著輸入資料大小的增加,演算法執行所需時間和空間的增長趨勢。這個定義有些拗口,我們可以將其分為三個重點來理解。