Sync zh and zh-hant versions (#1801)

* Sync zh and zh-hant versions.

* Unifying "数据体量" -> "数据规模".
This commit is contained in:
Yudong Jin
2025-08-28 04:51:02 +08:00
committed by GitHub
parent 803c0e09c7
commit 7b320e14fe
12 changed files with 48 additions and 54 deletions

View File

@ -26,10 +26,10 @@
由于实际测试具有较大的局限性,我们可以考虑仅通过一些计算来评估算法的效率。这种估算方法被称为<u>渐近复杂度分析asymptotic complexity analysis</u>,简称<u>复杂度分析</u>
复杂度分析能够体现算法运行所需的时间和空间资源与输入数据体量之间的关系。**它描述了随着输入数据体量的增加,算法执行所需时间和空间的增长趋势**。这个定义有些拗口,我们可以将其分为三个重点来理解。
复杂度分析能够体现算法运行所需的时间和空间资源与输入数据规模之间的关系。**它描述了随着输入数据规模的增加,算法执行所需时间和空间的增长趋势**。这个定义有些拗口,我们可以将其分为三个重点来理解。
- “时间和空间资源”分别对应<u>时间复杂度time complexity</u><u>空间复杂度space complexity</u>
- “随着输入数据体量的增加”意味着复杂度反映了算法运行效率与输入数据体量之间的关系。
- “随着输入数据规模的增加”意味着复杂度反映了算法运行效率与输入数据规模之间的关系。
- “时间和空间的增长趋势”表示复杂度分析关注的不是运行时间或占用空间的具体值,而是时间或空间增长的“快慢”。
**复杂度分析克服了实际测试方法的弊端**,体现在以下几个方面。

View File

@ -55,7 +55,7 @@
| 数据预处理 | / | 排序 $O(n \log n)$ | 建树 $O(n \log n)$ | 建哈希表 $O(n)$ |
| 数据是否有序 | 无序 | 有序 | 有序 | 无序 |
搜索算法的选择还取决于数据体量、搜索性能要求、数据查询与更新频率等。
搜索算法的选择还取决规模、搜索性能要求、数据查询与更新频率等。
**线性搜索**

View File

@ -3,6 +3,6 @@
- 二分查找依赖数据的有序性,通过循环逐步缩减一半搜索区间来进行查找。它要求输入数据有序,且仅适用于数组或基于数组实现的数据结构。
- 暴力搜索通过遍历数据结构来定位数据。线性搜索适用于数组和链表,广度优先搜索和深度优先搜索适用于图和树。此类算法通用性好,无须对数据进行预处理,但时间复杂度 $O(n)$ 较高。
- 哈希查找、树查找和二分查找属于高效搜索方法,可在特定数据结构中快速定位目标元素。此类算法效率高,时间复杂度可达 $O(\log n)$ 甚至 $O(1)$ ,但通常需要借助额外数据结构。
- 实际中,我们需要对数据体量、搜索性能要求、数据查询和更新频率等因素进行具体分析,从而选择合适的搜索方法。
- 实际中,我们需要对数据规模、搜索性能要求、数据查询和更新频率等因素进行具体分析,从而选择合适的搜索方法。
- 线性搜索适用于小型或频繁更新的数据;二分查找适用于大型、排序的数据;哈希查找适用于对查询效率要求较高且无须范围查询的数据;树查找适用于需要维护顺序和支持范围查询的大型动态数据。
- 用哈希查找替换线性查找是一种常用的优化运行时间的策略,可将时间复杂度从 $O(n)$ 降至 $O(1)$ 。

View File

@ -90,7 +90,7 @@ impl MyList {
panic!("索引越界")
};
let num = self.arr[index];
// 將索引 index 之後的元素都向前移動一位
// 將索引 index 之後的元素都向前移動一位
for j in index..self.size - 1 {
self.arr[j] = self.arr[j + 1];
}

View File

@ -13,22 +13,12 @@ fn test_push_max(heap: &mut BinaryHeap<i32>, val: i32) {
println!("\n元素 {} 入堆積後", val);
print_util::print_heap(heap.iter().map(|&val| val).collect());
}
fn test_push_min(heap: &mut BinaryHeap<Reverse<i32>>, val: i32) {
heap.push(Reverse(val)); // 元素入堆積
println!("\n元素 {} 入堆積後", val);
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
}
fn test_pop_max(heap: &mut BinaryHeap<i32>) {
let val = heap.pop().unwrap();
println!("\n堆積頂元素 {} 出堆積後", val);
print_util::print_heap(heap.iter().map(|&val| val).collect());
}
fn test_pop_min(heap: &mut BinaryHeap<Reverse<i32>>) {
let val = heap.pop().unwrap().0;
println!("\n堆積頂元素 {} 出堆積後", val);
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
}
/* Driver Code */
fn main() {

View File

@ -5,17 +5,17 @@
*/
use hello_algo_rust::include::print_util;
/* 基於環形陣列實現的雙向佇列 */
struct ArrayDeque {
nums: Vec<i32>, // 用於儲存雙向佇列元素的陣列
struct ArrayDeque<T> {
nums: Vec<T>, // 用於儲存雙向佇列元素的陣列
front: usize, // 佇列首指標,指向佇列首元素
que_size: usize, // 雙向佇列長度
}
impl ArrayDeque {
impl<T: Copy + Default> ArrayDeque<T> {
/* 建構子 */
pub fn new(capacity: usize) -> Self {
Self {
nums: vec![0; capacity],
nums: vec![T::default(); capacity],
front: 0,
que_size: 0,
}
@ -41,11 +41,11 @@ impl ArrayDeque {
// 透過取餘操作實現陣列首尾相連
// 當 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;
@ -59,7 +59,7 @@ impl ArrayDeque {
}
/* 佇列尾入列 */
pub fn push_last(&mut self, num: i32) {
pub fn push_last(&mut self, num: T) {
if self.que_size == self.capacity() {
println!("雙向佇列已滿");
return;
@ -72,7 +72,7 @@ impl ArrayDeque {
}
/* 佇列首出列 */
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);
@ -81,14 +81,14 @@ impl ArrayDeque {
}
/* 佇列尾出列 */
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!("雙向佇列為空")
};
@ -96,7 +96,7 @@ impl ArrayDeque {
}
/* 訪問佇列尾元素 */
fn peek_last(&self) -> i32 {
fn peek_last(&self) -> T {
if self.is_empty() {
panic!("雙向佇列為空")
};
@ -106,9 +106,9 @@ impl ArrayDeque {
}
/* 返回陣列用於列印 */
fn to_array(&self) -> Vec<i32> {
fn to_array(&self) -> Vec<T> {
// 僅轉換有效長度範圍內的串列元素
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)];

View File

@ -5,18 +5,18 @@
*/
/* 基於環形陣列實現的佇列 */
struct ArrayQueue {
nums: Vec<i32>, // 用於儲存佇列元素的陣列
struct ArrayQueue<T> {
nums: Vec<T>, // 用於儲存佇列元素的陣列
front: i32, // 佇列首指標,指向佇列首元素
que_size: i32, // 佇列長度
que_capacity: i32, // 佇列容量
}
impl ArrayQueue {
impl<T: Copy + Default> ArrayQueue<T> {
/* 建構子 */
fn new(capacity: i32) -> ArrayQueue {
fn new(capacity: i32) -> ArrayQueue<T> {
ArrayQueue {
nums: vec![0; capacity as usize],
nums: vec![T::default(); capacity as usize],
front: 0,
que_size: 0,
que_capacity: capacity,
@ -39,7 +39,7 @@ impl ArrayQueue {
}
/* 入列 */
fn push(&mut self, num: i32) {
fn push(&mut self, num: T) {
if self.que_size == self.capacity() {
println!("佇列已滿");
return;
@ -53,7 +53,7 @@ impl ArrayQueue {
}
/* 出列 */
fn pop(&mut self) -> i32 {
fn pop(&mut self) -> T {
let num = self.peek();
// 佇列首指標向後移動一位,若越過尾部,則返回到陣列頭部
self.front = (self.front + 1) % self.que_capacity;
@ -62,7 +62,7 @@ impl ArrayQueue {
}
/* 訪問佇列首元素 */
fn peek(&self) -> i32 {
fn peek(&self) -> T {
if self.is_empty() {
panic!("index out of bounds");
}
@ -70,10 +70,10 @@ impl ArrayQueue {
}
/* 返回陣列 */
fn to_vector(&self) -> Vec<i32> {
fn to_vector(&self) -> Vec<T> {
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;

View File

@ -50,11 +50,11 @@ impl<T: Copy> LinkedListDeque<T> {
/* 判斷雙向佇列是否為空 */
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 {
@ -102,7 +102,7 @@ impl<T: Copy> LinkedListDeque<T> {
}
/* 出列操作 */
pub fn pop(&mut self, is_front: bool) -> Option<T> {
fn pop(&mut self, is_front: bool) -> Option<T> {
// 若佇列為空,直接返回 None
if self.is_empty() {
return None;

View File

@ -33,7 +33,7 @@ impl<T: Copy> LinkedListQueue<T> {
/* 判斷佇列是否為空 */
pub fn is_empty(&self) -> bool {
return self.size() == 0;
return self.que_size == 0;
}
/* 入列 */

View File

@ -58,13 +58,17 @@ impl<T: Copy> LinkedListStack<T> {
}
/* 將 List 轉化為 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.push(node.borrow().val);
return nums;
pub fn to_array(&self) -> Vec<T> {
fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
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())
}
}
@ -80,7 +84,7 @@ fn main() {
stack.push(5);
stack.push(4);
print!("堆疊 stack = ");
print_util::print_array(&stack.to_array(stack.peek()));
print_util::print_array(&stack.to_array());
/* 訪問堆疊頂元素 */
let peek = stack.peek().unwrap().borrow().val;
@ -89,7 +93,7 @@ fn main() {
/* 元素出堆疊 */
let pop = stack.pop().unwrap();
print!("\n出堆疊元素 pop = {},出堆疊後 stack = ", pop);
print_util::print_array(&stack.to_array(stack.peek()));
print_util::print_array(&stack.to_array());
/* 獲取堆疊的長度 */
let size = stack.size();

View File

@ -34,7 +34,7 @@ impl TreeNode {
macro_rules! op_vec {
( $( $x:expr ),* ) => {
vec![
$( Option::from($x).map(|x| x) ),*
$(Option::from($x)),*
]
};
}

View File

@ -751,7 +751,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))$ 。
如下圖所示,計算漸近上界就是尋找一個函式 $f(n)$ ,使得當 $n$ 趨向於無窮大時,$T(n)$ 和 $f(n)$ 處於相同的增長級別,僅相差一個常數 $c$ 的倍數
如下圖所示,計算漸近上界就是尋找一個函式 $f(n)$ ,使得當 $n$ 趨向於無窮大時,$T(n)$ 和 $f(n)$ 處於相同的增長級別,僅相差一個常數係數 $c$。
![函式的漸近上界](time_complexity.assets/asymptotic_upper_bound.png)
@ -763,9 +763,9 @@ $T(n)$ 是一次函式,說明其執行時間的增長趨勢是線性的,因
### 第一步:統計操作數量
針對程式碼,逐行從上到下計算即可。然而,由於上述 $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.` 點的技巧。