Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -1,10 +1,10 @@
# Exercises
## Concept Review
## Check your understanding
### Which Element Leaves a Stack or Queue First?
### Which element comes out first in a stack or queue?
Prepare an empty stack `S` and an empty queue `Q`. Perform the same sequence of operations on each one:
Start with an empty stack `S` and an empty queue `Q`. Perform the same operations on each:
Step 1: Add `A`.
Step 2: Add `B`.
@@ -12,46 +12,46 @@ Step 3: Remove and record one element.
Step 4: Add `C`.
Step 5: Keep removing and recording elements until the container is empty.
Write the order in which elements are removed from `S` and from `Q`. Explain the difference using "last-in-first-out" or "first-in-first-out."
Write the order in which elements are removed from `S` and from `Q`. Explain the difference using last in, first out or first in, first out.
??? success "Answer"
??? success "Reference answer"
The removal order for stack `S` is `B, C, A`. After the elements `A, B` are added, the most recently added element, `B`, is popped first. After `C` is added,
`C, A` are popped in that order. This is "last-in-first-out."
The removal order for stack `S` is `B, C, A`. After adding `A, B`, we first pop `B`, the most recently added element, and then add `C`.
We then pop `C, A` in order. This is last in, first out.
The removal order for queue `Q` is `A, B, C`. After the elements `A, B` are added, the earliest added element, `A`, is removed first.
After `C` is added, `B, C` are removed in that order. This is "first-in-first-out."
The removal order for queue `Q` is `A, B, C`. After adding `A, B`, we first remove `A`, the earliest element added.
We then add `C` and remove `B, C` in order. This is first in, first out.
### What Happens When the Rear Passes the End of the Array?
### What happens when the rear passes the end of the array?
A queue is implemented with a circular array of length 5, whose indices are `0–4`.
Currently, `front = 3` and `size = 2`; `A, B` are stored at indices 3 and 4, respectively.
A queue uses a circular array of length 5, with indices `0-4`.
Currently, `front = 3` and `size = 2`, with queue elements `A, B` at indices 3 and 4, respectively.
<!-- numbered-subquestions -->
1. When `C` is enqueued, at which index should `C` be stored? What is `size` after the enqueue?
2. Next, dequeue once. Which element is removed? What are the new values of `front` and `size`?
3. What is the logical order from the front to the rear now? Does dequeuing require moving the other elements in the array? Why or why not?
1. When enqueueing `C`, at which index should `C` be placed? What is `size` afterward?
2. If we then dequeue once, which element is removed? What are the new values of `front` and `size`?
3. What is the logical order from front to rear now? Does dequeuing require moving the other array elements? Why?
??? success "Answer"
??? success "Reference answer"
1. The new element's position is
`(front + size) % 5 = (3 + 2) % 5 = 0`,
so `C` is stored at index 0. After the enqueue, `size = 3`.
so `C` goes at index 0. After enqueueing, `size = 3`.
2. Dequeuing removes the current front element, `A`. The new front index is
2. Dequeueing removes the current front element, `A`. The new front index is
`(3 + 1) % 5 = 4`, so `front = 4` and `size = 2`.
3. The logical order of valid elements is `B, C`, with `B` at index 4 and `C` at index 0.
Dequeuing requires only changing `front` and `size`. The circular array uses the remainder operation to wrap the index back to the beginning,
so there is no need to shift all the other elements forward.
3. The valid elements are logically ordered as `B, C`, with `B` at index 4 and `C` at index 0.
Dequeueing only advances `front` and updates `size`. The circular array uses the remainder operation to wrap indices back to the beginning,
so the other elements do not need to shift forward together.
### Operations at Both Ends of a Deque
### Operations at both ends of a deque
Here, `push_first` adds an element at the front, `push_last` adds one at the rear,
`pop_first` removes one from the front, and `pop_last` removes one from the rear.
Here, `push_first` adds at the front, `push_last` adds at the rear,
`pop_first` removes from the front, and `pop_last` removes from the rear.
Perform the following operations on an empty deque `deq`:
Starting with an empty deque `deq`, perform:
1. `push_last(A)`
2. `push_last(B)`
@@ -62,38 +62,38 @@ Perform the following operations on an empty deque `deq`:
<!-- numbered-subquestions -->
1. Which elements are returned by the two pop operations?
2. After all operations are complete, which elements remain from front to rear?
3. Examine the six operations. Can a queue that allows insertion only at the rear and removal only at the front perform all of them? If not, identify the operations it cannot perform. Then state whether a deque can perform them and explain why.
1. Which elements are removed by the two pop operations?
2. Which elements remain, from front to rear, after all the operations?
3. Examine these six steps. Can a queue that allows additions only at the rear and removals only at the front perform them all? If not, identify the unsupported operations. Then explain whether a deque can perform them all, and why.
??? success "Answer"
??? success "Reference answer"
After the first three steps, the deque from front to rear is `[C, A, B]`.
After the first three steps, the deque contains `[C, A, B]` from front to rear.
<!-- numbered-subquestions -->
1. `pop_last()` removes `B`. After `D` is added, the deque is `[C, A, D]`,
and `pop_first()` then removes `C`.
1. `pop_last()` removes `B`. After adding `D`, the deque is `[C, A, D]`.
Then `pop_first()` removes `C`.
2. `[A, D]` remains.
2. The remaining elements are `[A, D]`.
3. A queue that permits insertion only at the rear and removal only at the front cannot perform all the operations.
Step 3, `push_first(C)`, requires insertion at the front, and Step 4, `pop_last()`, requires removal at the rear. Both are outside the operations supported by such a queue.
A deque permits insertion and removal at both ends, so it can perform all six operations.
3. A queue that allows additions only at the rear and removals only at the front cannot perform every operation:
step 3, `push_first(C)`, adds at the front, and step 4, `pop_last()`, removes from the rear. Neither operation is supported by that queue.
A deque supports additions and removals at both ends, so it can perform all six steps.
## Programming Exercises
## Programming exercises
### Check a Bracket Sequence
### Checking a bracket sequence
Given a string `s` containing only the three types of brackets `()`, `[]`, and `{}`, use a stack to determine whether the string is valid.
Given a string `s` containing only the three types of brackets `()`, `[]`, and `{}`, use a stack to determine whether it is valid.
A valid sequence must satisfy both conditions: every closing bracket matches the type of the most recent unmatched opening bracket,
and no unmatched opening bracket remains after the traversal. Return a Boolean value for the result.
A valid sequence must satisfy both conditions: each closing bracket matches the type of the most recent unmatched opening bracket,
and no unmatched opening brackets remain at the end. Return a Boolean result.
??? tip "Hints"
1. You can create a mapping from each closing bracket to its matching opening bracket
2. When you encounter a closing bracket, first check whether the stack is empty, and then check whether the top matches
3. The stack must also be empty after the traversal
1. You can map each closing bracket to its matching opening bracket.
2. When you encounter a closing bracket, first check whether the stack is empty, then check whether its top matches.
3. The stack must also be empty when the scan finishes.
[LeetCode](https://leetcode.com/problems/valid-parentheses/){ .rounded-button .exercise-button target="_blank" rel="noopener noreferrer" }