diff --git a/zh-hant/codes/go/chapter_stack_and_queue/array_deque.go b/zh-hant/codes/go/chapter_stack_and_queue/array_deque.go index 89be939b6..4cf6632bd 100644 --- a/zh-hant/codes/go/chapter_stack_and_queue/array_deque.go +++ b/zh-hant/codes/go/chapter_stack_and_queue/array_deque.go @@ -72,6 +72,9 @@ func (q *arrayDeque) pushLast(num int) { /* 佇列首出列 */ func (q *arrayDeque) popFirst() any { num := q.peekFirst() + if num == nil { + return nil + } // 佇列首指標向後移動一位 q.front = q.index(q.front + 1) q.queSize-- @@ -81,6 +84,9 @@ func (q *arrayDeque) popFirst() any { /* 佇列尾出列 */ func (q *arrayDeque) popLast() any { num := q.peekLast() + if num == nil { + return nil + } q.queSize-- return num } diff --git a/zh-hant/codes/go/chapter_stack_and_queue/array_queue.go b/zh-hant/codes/go/chapter_stack_and_queue/array_queue.go index a719e5513..699fe4d79 100644 --- a/zh-hant/codes/go/chapter_stack_and_queue/array_queue.go +++ b/zh-hant/codes/go/chapter_stack_and_queue/array_queue.go @@ -49,6 +49,10 @@ func (q *arrayQueue) push(num int) { /* 出列 */ func (q *arrayQueue) pop() any { num := q.peek() + if num == nil { + return nil + } + // 佇列首指標向後移動一位,若越過尾部,則返回到陣列頭部 q.front = (q.front + 1) % q.queCapacity q.queSize-- diff --git a/zh-hant/codes/go/chapter_stack_and_queue/queue_test.go b/zh-hant/codes/go/chapter_stack_and_queue/queue_test.go index ec216f33b..8eca50e4b 100644 --- a/zh-hant/codes/go/chapter_stack_and_queue/queue_test.go +++ b/zh-hant/codes/go/chapter_stack_and_queue/queue_test.go @@ -46,9 +46,13 @@ func TestQueue(t *testing.T) { } func TestArrayQueue(t *testing.T) { + // 初始化佇列,使用佇列的通用介面 capacity := 10 queue := newArrayQueue(capacity) + if queue.pop() != nil { + t.Errorf("want:%v,got:%v", nil, queue.pop()) + } // 元素入列 queue.push(1) diff --git a/zh-hant/codes/rust/chapter_backtracking/permutations_i.rs b/zh-hant/codes/rust/chapter_backtracking/permutations_i.rs index 090f57d95..7b2f10bfa 100644 --- a/zh-hant/codes/rust/chapter_backtracking/permutations_i.rs +++ b/zh-hant/codes/rust/chapter_backtracking/permutations_i.rs @@ -23,7 +23,7 @@ fn backtrack(mut state: Vec, choices: &[i32], selected: &mut [bool], res: & backtrack(state.clone(), choices, selected, res); // 回退:撤銷選擇,恢復到之前的狀態 selected[i] = false; - state.remove(state.len() - 1); + state.pop(); } } } diff --git a/zh-hant/codes/rust/chapter_backtracking/permutations_ii.rs b/zh-hant/codes/rust/chapter_backtracking/permutations_ii.rs index d2a97642a..8a5e85269 100644 --- a/zh-hant/codes/rust/chapter_backtracking/permutations_ii.rs +++ b/zh-hant/codes/rust/chapter_backtracking/permutations_ii.rs @@ -27,7 +27,7 @@ fn backtrack(mut state: Vec, choices: &[i32], selected: &mut [bool], res: & backtrack(state.clone(), choices, selected, res); // 回退:撤銷選擇,恢復到之前的狀態 selected[i] = false; - state.remove(state.len() - 1); + state.pop(); } } } diff --git a/zh-hant/docs/index.html b/zh-hant/docs/index.html index 578ddaca8..de1acac46 100644 --- a/zh-hant/docs/index.html +++ b/zh-hant/docs/index.html @@ -286,6 +286,14 @@
Zig, Rust +