概述:《操作历史管理器的撤销/重做能力》(题号
OD_1079)是 2026.04.29 考期华为 OD 机考 100分 真题,考查 链表,采用 Core 核心代码模式(无需手写输入输出)。时间复杂度 O(n),空间复杂度 O(m),下文给出具体推导步骤、边界排查与 Python、Java、C++ 实现。
2026年最新华为OD机考题库(按考点分类): 2026年最新华为OD机考题库
一、真题档案
| 维度 | 详情 |
|---|---|
| 题号 / 题名 | OD_1079 操作历史管理器的撤销/重做能力 |
| 考期 / 分值 | 2026.04.29 / 100分 |
| 所属考点 | 链表 |
| 做题模式 | Core 核心代码模式(无需手写输入输出) |
| 关联力扣 Hot100 | LRU 缓存 |
| 同类考点题目 | 手机桌面整理 |
二、题目描述
实现一个操作历史管理器,使用双向链表存储执行过的操作。支持执行新操作、撤销和重做功能。
- 执行操作(execute {操作描述}):执行新操作,并清除当前操作之后的所有历史记录
- 撤销(undo):回退到上一个操作状态(上一个操作状态可以为从未执行过任何操作的状态,若当前状态已经是从未执行过任何操作的状态,则 undo 失败)
- 重做(redo):前进到下一个操作状态(下一个操作状态是之前撤销过的操作,若没有进行过撤销操作(即链表的下一个操作状态不存在),则 redo 失败)
输入说明
参数 commands 是二维字符串数组。执行命令表示为 [“execute”,“操作描述”],撤销和重做分别表示为 [“undo”]、[“redo”]。[[]] 表示没有有效命令,空命令会被忽略。
输出说明
正常执行完全部命令后,返回当前操作描述;未执行过任何操作时返回空字符串。若在初始状态继续撤销,立即返回 “undo failed”;若不存在可重做的操作,立即返回 “redo failed”,并停止处理后续命令。
示例
样例1
输入
[["execute", "insert hello"], ["execute", "newline"], ["execute", "insert woo"], ["undo"], ["execute", "insert world"], ["undo"]]
输出
"newline"
说明
- 执行 insert hello → 当前:insert hello
- 执行 newline → 当前:newline
- 执行 insert woo → 当前:insert woo
- 撤销 → 当前:newline(当前回滚到上一步的状态)
- 执行 insert world → 当前:insert world(清除任何后续历史)
- 撤销 → 当前:newline
样例2
输入
[[]]
输出
""
说明
当前状态是从未执行过任何操作,输出: “”
样例3
输入
[["execute", "insert hello"], ["undo"]]
输出
""
说明
- 执行 insert hello → 当前:insert hello
- 撤销 → 当前:“”(当前状态是从未执行过任何操作,输出: “”)
样例4
输入
[["execute","insert hello"],["undo"],["redo"]]
输出
"insert hello"
说明
- 执行 insert hello → 当前:insert hello
- 撤销 → 当前:“”
- 重做 → 当前:insert hello
样例5
输入
[["execute","insert V"],["execute","insert W"],["execute","insert A"],["undo"],["undo"],["execute","insert IEW"],["redo"]]
输出
"redo failed"
说明
- 执行 insert V → 当前 insert V
- 执行 insert W → 当前:insert W
- 执行 insert A → 当前:insert A
- 撤销 → 当前:insert W(当前回滚到上一步的状态)
- 撤销 → 当前:insert V(当前回滚到上一步的状态)
- 执行 insert IEW → 当前:insert IEW(清除任何后续历史)
- 重做 → 重做失败,输出"redo failed",由于上一步已经清空操作历史,所以重做失败
三、解题思路
第一步:用双向链表记历史
把执行过的操作排成一串,每个操作是一个节点,节点里存操作描述,还有两个指针:prev 指向上一个操作,next 指向下一个操作。这样一串互相指着的节点叫双向链表。
再用一个指针 current 指向“当前状态”所在的节点。
第二步:头哨兵
“从来没执行过任何操作”也是一种状态,撤销可以撤到这里。为了统一处理,在链表最前面放一个不代表任何操作的空节点 head,描述是空字符串,叫哨兵节点。一开始 current = head。
这样“回到初始状态”就是 current 回到 head,不用单独判断“链表是不是空的”。
第三步:三种命令
- execute 操作描述:新操作会让“之前撤销掉、还能重做的那些操作”作废。所以先令 current.next = 空,把后面那一串整个断开;再新建一个节点接在 current 后面,current 移到新节点;
- undo:current 已经是 head,没得撤,立刻返回 “undo failed”;否则 current 移到 current.prev;
- redo:current.next 是空的,没得重做,立刻返回 “redo failed”;否则 current 移到 current.next;
- 空命令直接跳过。
全部命令处理完,返回 current 的描述(在 head 时是空字符串)。
撤销只是移动 current,后面的节点还挂在链表上,所以之后还能重做;只有执行新操作时才把它们断开。
用样例 1 走一遍
| 命令 | 链表(head 之后) | current 指向 |
|---|---|---|
| execute insert hello | insert hello | insert hello |
| execute newline | insert hello → newline | newline |
| execute insert woo | insert hello → newline → insert woo | insert woo |
| undo | 不变 | newline |
| execute insert world | insert hello → newline → insert world(insert woo 被断开) | insert world |
| undo | 不变 | newline |
结果 “newline”。
样例 3:执行 insert hello 后 undo,current 回到 head,结果是空字符串。如果此时再 undo 一次,才会返回 “undo failed”。
四、高频 WA 易错点(边界排查)
- 撤销到初始空状态是合法的,只有在空状态还要撤销才失败。
- execute 要断开 current 后面的整串历史,不是只删一个节点。
- undo 或 redo 失败时要立刻返回,不再处理后面的命令。
- 当前在 head 时返回空字符串,不是 null。
- 空命令(如 [[]])直接忽略。
五、时间与空间复杂度
时间复杂度:O(n),n 是命令数,每条命令只移动、修改常数个指针。
空间复杂度:O(m),m 是创建过的节点数。
六、参考代码实现
以下提供 Python、Java、C++ 核心实现(已按题目输入输出约定与边界用例自测)。
Python 实现
class Solution:
def manageHistory(self, commands):
class Node:
def __init__(self, description):
self.description = description
self.prev = None
self.next = None
# 头哨兵表示“从未执行过任何操作”的状态
head = Node("")
current = head
for command in commands:
# 兼容题目中用 [[]] 表示没有有效命令的情况
if not command:
continue
action = command[0]
if action == "execute":
# 新操作会清除当前节点之后的全部可重做历史
current.next = None
node = Node(command[1])
node.prev = current
current.next = node
current = node
elif action == "undo":
if current is head:
return "undo failed"
current = current.prev
else:
if current.next is None:
return "redo failed"
current = current.next
return current.description
Java 实现
class Solution {
private static class Node {
String description;
Node prev;
Node next;
Node(String description) {
this.description = description;
}
}
public String manageHistory(String[][] commands) {
// 头哨兵同时表示“从未执行过任何操作”的状态
Node head = new Node("");
Node current = head;
for (String[] command : commands) {
// 兼容题目中用空数组表示没有有效命令的情况
if (command.length == 0) continue;
String action = command[0];
if ("execute".equals(action)) {
// 覆盖当前后继,旧的可重做历史将不再可达
current.next = null;
Node node = new Node(command[1]);
node.prev = current;
current.next = node;
current = node;
} else if ("undo".equals(action)) {
if (current == head) return "undo failed";
current = current.prev;
} else {
if (current.next == null) return "redo failed";
current = current.next;
}
}
return current.description;
}
}
C++ 实现
class Solution {
struct Node {
string description;
Node* prev;
Node* next;
Node(const string& text) : description(text), prev(nullptr), next(nullptr) {}
};
public:
string manageHistory(vector<vector<string>> commands) {
// 统一记录动态节点,结束前集中释放,避免内存泄漏
vector<Node*> allocated;
Node* head = new Node("");
allocated.push_back(head);
Node* current = head;
auto finish = [&](string answer) {
// answer 按值复制,释放节点后仍可安全返回
for (Node* node : allocated) delete node;
return answer;
};
for (const auto& command : commands) {
// 兼容题目中用 [[]] 表示没有有效命令的情况
if (command.empty()) continue;
const string& action = command[0];
if (action == "execute") {
// 断开旧后继,即清除当前状态之后的重做历史
current->next = nullptr;
Node* node = new Node(command[1]);
allocated.push_back(node);
node->prev = current;
current->next = node;
current = node;
} else if (action == "undo") {
if (current == head) return finish("undo failed");
current = current->prev;
} else {
if (current->next == nullptr) return finish("redo failed");
current = current->next;
}
}
return finish(current->description);
}
};
七、同类考点巩固与练习参考
- 边界自测清单:提交前建议重点手写验证极小规模(长度为 1 或空输入)、重复元素、边界等号条件以及大数溢出等情况,避免只过题面样例。
- 同类题连续巩固:掌握本题后,可结合同类考点题(手机桌面整理)及力扣 Hot100 原题(LRU 缓存)进一步巩固「链表」题型。
- 练习参考:如需在线提交评测,可参考题目练习页:OD_1079 操作历史管理器的撤销/重做能力。
八、常见问题(FAQ)
Q1:《操作历史管理器的撤销/重做能力》在华为 OD 机考里属于什么难度和题型?
该题在 2026.04.29 考期分值为 100分,核心考点为 链表,判题模式为 Core 核心代码模式(无需手写输入输出)。
Q2:本地样例通过但提交报 WA(答案错误)通常怎么排查?
建议优先核对本文第四节的边界坑点,依次检查:下标从 0 还是 1 开始、比较条件是严格大于/小于还是包含等于、重复值处理、极端最小/最大数据范围,以及输出格式中的空格或换行符。
Q3:Core 核心代码模式和 ACM 模式在编写代码时有什么区别?
Core 核心代码模式只需实现给定的类和方法签名并按要求返回结果,无需手写标准输入解析与打印;ACM 模式则需要自行从标准输入读取多行或不定长数据并按约定格式输出,实际机考以题目页面给出的模板为准。
8万+


链表解析与PythonJavaC++等多语言代码&spm=1001.2101.3001.11974&articleId=171941862&d=1&t=3&u=9adc019fa90d440a880d7db7b77c9a92)

被折叠的 条评论
为什么被折叠?



