Skip to content

Commit 7b320e1

Browse files
authored
Sync zh and zh-hant versions (#1801)
* Sync zh and zh-hant versions. * Unifying "数据体量" -> "数据规模".
1 parent 803c0e0 commit 7b320e1

File tree

12 files changed

+48
-54
lines changed

12 files changed

+48
-54
lines changed

docs/chapter_computational_complexity/performance_evaluation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

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

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

3131
- “时间和空间资源”分别对应<u>时间复杂度(time complexity)</u>和<u>空间复杂度(space complexity)</u>。
32-
-随着输入数据体量的增加”意味着复杂度反映了算法运行效率与输入数据体量之间的关系
32+
-随着输入数据规模的增加”意味着复杂度反映了算法运行效率与输入数据规模之间的关系
3333
- “时间和空间的增长趋势”表示复杂度分析关注的不是运行时间或占用空间的具体值,而是时间或空间增长的“快慢”。
3434

3535
**复杂度分析克服了实际测试方法的弊端**,体现在以下几个方面。

docs/chapter_searching/searching_algorithm_revisited.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
| 数据预处理 | / | 排序 $O(n \log n)$ | 建树 $O(n \log n)$ | 建哈希表 $O(n)$ |
5656
| 数据是否有序 | 无序 | 有序 | 有序 | 无序 |
5757

58-
搜索算法的选择还取决于数据体量、搜索性能要求、数据查询与更新频率等。
58+
搜索算法的选择还取决规模、搜索性能要求、数据查询与更新频率等。
5959

6060
**线性搜索**
6161

docs/chapter_searching/summary.md

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

zh-hant/codes/rust/chapter_array_and_linkedlist/my_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl MyList {
9090
panic!("索引越界")
9191
};
9292
let num = self.arr[index];
93-
// 將將索引 index 之後的元素都向前移動一位
93+
// 將索引 index 之後的元素都向前移動一位
9494
for j in index..self.size - 1 {
9595
self.arr[j] = self.arr[j + 1];
9696
}

zh-hant/codes/rust/chapter_heap/heap.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,12 @@ fn test_push_max(heap: &mut BinaryHeap<i32>, val: i32) {
1313
println!("\n元素 {} 入堆積後", val);
1414
print_util::print_heap(heap.iter().map(|&val| val).collect());
1515
}
16-
fn test_push_min(heap: &mut BinaryHeap<Reverse<i32>>, val: i32) {
17-
heap.push(Reverse(val)); // 元素入堆積
18-
println!("\n元素 {} 入堆積後", val);
19-
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
20-
}
2116

2217
fn test_pop_max(heap: &mut BinaryHeap<i32>) {
2318
let val = heap.pop().unwrap();
2419
println!("\n堆積頂元素 {} 出堆積後", val);
2520
print_util::print_heap(heap.iter().map(|&val| val).collect());
2621
}
27-
fn test_pop_min(heap: &mut BinaryHeap<Reverse<i32>>) {
28-
let val = heap.pop().unwrap().0;
29-
println!("\n堆積頂元素 {} 出堆積後", val);
30-
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
31-
}
3222

3323
/* Driver Code */
3424
fn main() {

zh-hant/codes/rust/chapter_stack_and_queue/array_deque.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
*/
66
use hello_algo_rust::include::print_util;
77
/* 基於環形陣列實現的雙向佇列 */
8-
struct ArrayDeque {
9-
nums: Vec<i32>, // 用於儲存雙向佇列元素的陣列
8+
struct ArrayDeque<T> {
9+
nums: Vec<T>, // 用於儲存雙向佇列元素的陣列
1010
front: usize, // 佇列首指標,指向佇列首元素
1111
que_size: usize, // 雙向佇列長度
1212
}
1313

14-
impl ArrayDeque {
14+
impl<T: Copy + Default> ArrayDeque<T> {
1515
/* 建構子 */
1616
pub fn new(capacity: usize) -> Self {
1717
Self {
18-
nums: vec![0; capacity],
18+
nums: vec![T::default(); capacity],
1919
front: 0,
2020
que_size: 0,
2121
}
@@ -41,11 +41,11 @@ impl ArrayDeque {
4141
// 透過取餘操作實現陣列首尾相連
4242
// 當 i 越過陣列尾部後,回到頭部
4343
// 當 i 越過陣列頭部後,回到尾部
44-
return ((i + self.capacity() as i32) % self.capacity() as i32) as usize;
44+
((i + self.capacity() as i32) % self.capacity() as i32) as usize
4545
}
4646

4747
/* 佇列首入列 */
48-
pub fn push_first(&mut self, num: i32) {
48+
pub fn push_first(&mut self, num: T) {
4949
if self.que_size == self.capacity() {
5050
println!("雙向佇列已滿");
5151
return;
@@ -59,7 +59,7 @@ impl ArrayDeque {
5959
}
6060

6161
/* 佇列尾入列 */
62-
pub fn push_last(&mut self, num: i32) {
62+
pub fn push_last(&mut self, num: T) {
6363
if self.que_size == self.capacity() {
6464
println!("雙向佇列已滿");
6565
return;
@@ -72,7 +72,7 @@ impl ArrayDeque {
7272
}
7373

7474
/* 佇列首出列 */
75-
fn pop_first(&mut self) -> i32 {
75+
fn pop_first(&mut self) -> T {
7676
let num = self.peek_first();
7777
// 佇列首指標向後移動一位
7878
self.front = self.index(self.front as i32 + 1);
@@ -81,22 +81,22 @@ impl ArrayDeque {
8181
}
8282

8383
/* 佇列尾出列 */
84-
fn pop_last(&mut self) -> i32 {
84+
fn pop_last(&mut self) -> T {
8585
let num = self.peek_last();
8686
self.que_size -= 1;
8787
num
8888
}
8989

9090
/* 訪問佇列首元素 */
91-
fn peek_first(&self) -> i32 {
91+
fn peek_first(&self) -> T {
9292
if self.is_empty() {
9393
panic!("雙向佇列為空")
9494
};
9595
self.nums[self.front]
9696
}
9797

9898
/* 訪問佇列尾元素 */
99-
fn peek_last(&self) -> i32 {
99+
fn peek_last(&self) -> T {
100100
if self.is_empty() {
101101
panic!("雙向佇列為空")
102102
};
@@ -106,9 +106,9 @@ impl ArrayDeque {
106106
}
107107

108108
/* 返回陣列用於列印 */
109-
fn to_array(&self) -> Vec<i32> {
109+
fn to_array(&self) -> Vec<T> {
110110
// 僅轉換有效長度範圍內的串列元素
111-
let mut res = vec![0; self.que_size];
111+
let mut res = vec![T::default(); self.que_size];
112112
let mut j = self.front;
113113
for i in 0..self.que_size {
114114
res[i] = self.nums[self.index(j as i32)];

zh-hant/codes/rust/chapter_stack_and_queue/array_queue.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
*/
66

77
/* 基於環形陣列實現的佇列 */
8-
struct ArrayQueue {
9-
nums: Vec<i32>, // 用於儲存佇列元素的陣列
8+
struct ArrayQueue<T> {
9+
nums: Vec<T>, // 用於儲存佇列元素的陣列
1010
front: i32, // 佇列首指標,指向佇列首元素
1111
que_size: i32, // 佇列長度
1212
que_capacity: i32, // 佇列容量
1313
}
1414

15-
impl ArrayQueue {
15+
impl<T: Copy + Default> ArrayQueue<T> {
1616
/* 建構子 */
17-
fn new(capacity: i32) -> ArrayQueue {
17+
fn new(capacity: i32) -> ArrayQueue<T> {
1818
ArrayQueue {
19-
nums: vec![0; capacity as usize],
19+
nums: vec![T::default(); capacity as usize],
2020
front: 0,
2121
que_size: 0,
2222
que_capacity: capacity,
@@ -39,7 +39,7 @@ impl ArrayQueue {
3939
}
4040

4141
/* 入列 */
42-
fn push(&mut self, num: i32) {
42+
fn push(&mut self, num: T) {
4343
if self.que_size == self.capacity() {
4444
println!("佇列已滿");
4545
return;
@@ -53,7 +53,7 @@ impl ArrayQueue {
5353
}
5454

5555
/* 出列 */
56-
fn pop(&mut self) -> i32 {
56+
fn pop(&mut self) -> T {
5757
let num = self.peek();
5858
// 佇列首指標向後移動一位,若越過尾部,則返回到陣列頭部
5959
self.front = (self.front + 1) % self.que_capacity;
@@ -62,18 +62,18 @@ impl ArrayQueue {
6262
}
6363

6464
/* 訪問佇列首元素 */
65-
fn peek(&self) -> i32 {
65+
fn peek(&self) -> T {
6666
if self.is_empty() {
6767
panic!("index out of bounds");
6868
}
6969
self.nums[self.front as usize]
7070
}
7171

7272
/* 返回陣列 */
73-
fn to_vector(&self) -> Vec<i32> {
73+
fn to_vector(&self) -> Vec<T> {
7474
let cap = self.que_capacity;
7575
let mut j = self.front;
76-
let mut arr = vec![0; self.que_size as usize];
76+
let mut arr = vec![T::default(); cap as usize];
7777
for i in 0..self.que_size {
7878
arr[i as usize] = self.nums[(j % cap) as usize];
7979
j += 1;

zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_deque.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ impl<T: Copy> LinkedListDeque<T> {
5050

5151
/* 判斷雙向佇列是否為空 */
5252
pub fn is_empty(&self) -> bool {
53-
return self.size() == 0;
53+
return self.que_size == 0;
5454
}
5555

5656
/* 入列操作 */
57-
pub fn push(&mut self, num: T, is_front: bool) {
57+
fn push(&mut self, num: T, is_front: bool) {
5858
let node = ListNode::new(num);
5959
// 佇列首入列操作
6060
if is_front {
@@ -102,7 +102,7 @@ impl<T: Copy> LinkedListDeque<T> {
102102
}
103103

104104
/* 出列操作 */
105-
pub fn pop(&mut self, is_front: bool) -> Option<T> {
105+
fn pop(&mut self, is_front: bool) -> Option<T> {
106106
// 若佇列為空,直接返回 None
107107
if self.is_empty() {
108108
return None;

zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<T: Copy> LinkedListQueue<T> {
3333

3434
/* 判斷佇列是否為空 */
3535
pub fn is_empty(&self) -> bool {
36-
return self.size() == 0;
36+
return self.que_size == 0;
3737
}
3838

3939
/* 入列 */

zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_stack.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ impl<T: Copy> LinkedListStack<T> {
5858
}
5959

6060
/* 將 List 轉化為 Array 並返回 */
61-
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
62-
if let Some(node) = head {
63-
let mut nums = self.to_array(node.borrow().next.as_ref());
64-
nums.push(node.borrow().val);
65-
return nums;
61+
pub fn to_array(&self) -> Vec<T> {
62+
fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
63+
if let Some(node) = head {
64+
let mut nums = _to_array(node.borrow().next.as_ref());
65+
nums.push(node.borrow().val);
66+
return nums;
67+
}
68+
return Vec::new();
6669
}
67-
return Vec::new();
70+
71+
_to_array(self.peek())
6872
}
6973
}
7074

@@ -80,7 +84,7 @@ fn main() {
8084
stack.push(5);
8185
stack.push(4);
8286
print!("堆疊 stack = ");
83-
print_util::print_array(&stack.to_array(stack.peek()));
87+
print_util::print_array(&stack.to_array());
8488

8589
/* 訪問堆疊頂元素 */
8690
let peek = stack.peek().unwrap().borrow().val;
@@ -89,7 +93,7 @@ fn main() {
8993
/* 元素出堆疊 */
9094
let pop = stack.pop().unwrap();
9195
print!("\n出堆疊元素 pop = {},出堆疊後 stack = ", pop);
92-
print_util::print_array(&stack.to_array(stack.peek()));
96+
print_util::print_array(&stack.to_array());
9397

9498
/* 獲取堆疊的長度 */
9599
let size = stack.size();

0 commit comments

Comments
 (0)