2296.设计一个文本编辑器
栈,双向链表,模拟,https://leetcode.cn/problems/design-a-text-editor/
请你设计一个带光标的文本编辑器,它可以实现以下功能:
- 添加:在光标所在处添加文本。
- 删除:在光标所在处删除文本(模拟键盘的删除键)。
- 移动:将光标往左或者往右移动。
当删除文本时,只有光标左边的字符会被删除。光标会留在文本内,也就是说任意时候 0 <= cursor.position <= currentText.length 都成立。
请你实现 TextEditor 类:
TextEditor()用空文本初始化对象。void addText(string text)将text添加到光标所在位置。添加完后光标在text的右边。int deleteText(int k)删除光标左边k个字符。返回实际删除的字符数目。string cursorLeft(int k)将光标向左移动k次。返回移动后光标左边min(10, len)个字符,其中len是光标左边的字符数目。string cursorRight(int k)将光标向右移动k次。返回移动后光标左边min(10, len)个字符,其中len是光标左边的字符数目。
示例 1:
输入:
["TextEditor", "addText", "deleteText", "addText", "cursorRight", "cursorLeft", "deleteText", "cursorLeft", "cursorRight"]
[[], ["leetcode"], [4], ["practice"], [3], [8], [10], [2], [6]]
输出:
[null, null, 4, null, "etpractice", "leet", 4, "", "practi"]
解释:
TextEditor textEditor = new TextEditor(); // 当前 text 为 "|" 。('|' 字符表示光标)
textEditor.addText("leetcode"); // 当前文本为 "leetcode|" 。
textEditor.deleteText(4); // 返回 4
// 当前文本为 "leet|" 。
// 删除了 4 个字符。
textEditor.addText("practice"); // 当前文本为 "leetpractice|" 。
textEditor.cursorRight(3); // 返回 "etpractice"
// 当前文本为 "leetpractice|".
// 光标无法移动到文本以外,所以无法移动。
// "etpractice" 是光标左边的 10 个字符。
textEditor.cursorLeft(8); // 返回 "leet"
// 当前文本为 "leet|practice" 。
// "leet" 是光标左边的 min(10, 4) = 4 个字符。
textEditor.deleteText(10); // 返回 4
// 当前文本为 "|practice" 。
// 只有 4 个字符被删除了。
textEditor.cursorLeft(2); // 返回 ""
// 当前文本为 "|practice" 。
// 光标无法移动到文本以外,所以无法移动。
// "" 是光标左边的 min(10, 0) = 0 个字符。
textEditor.cursorRight(6); // 返回 "practi"
// 当前文本为 "practi|ce" 。
// "practi" 是光标左边的 min(10, 6) = 6 个字符。提示:
1 <= text.length, k <= 40text只含有小写英文字母。- 调用
addText,deleteText,cursorLeft和cursorRight的 总 次数不超过2 * 10^4次。
进阶:你能设计并实现一个每次调用时间复杂度为 O(k) 的解决方案吗?
下面给出一种利用左右栈模拟光标移动的 Python 解法,每次操作的时间复杂度为 O(k):
python
class TextEditor:
def __init__(self):
# 左侧栈存储光标左边的字符(顺序与原字符串一致)
# 右侧栈存储光标右边的字符(顺序为逆序,方便移动光标时恢复原序)
self.left = []
self.right = []
def addText(self, text: str) -> None:
# 将文本中的字符逐个添加到左侧栈,光标始终在文本的最右侧
for ch in text:
self.left.append(ch)
def deleteText(self, k: int) -> int:
# 删除光标左边的 k 个字符
count = 0
while k > 0 and self.left:
self.left.pop()
count += 1
k -= 1
return count
def cursorLeft(self, k: int) -> str:
# 将光标向左移动 k 次,每次将左侧栈的栈顶字符移动到右侧栈
while k > 0 and self.left:
self.right.append(self.left.pop())
k -= 1
# 返回光标左边最多 10 个字符(原顺序)
return ''.join(self.left[-10:])
def cursorRight(self, k: int) -> str:
# 将光标向右移动 k 次,每次将右侧栈的栈顶字符移动回左侧栈
while k > 0 and self.right:
self.left.append(self.right.pop())
k -= 1
# 返回光标左边最多 10 个字符(原顺序)
return ''.join(self.left[-10:])说明
- 数据结构:
- 使用两个列表模拟左右栈,其中
self.left存放光标左侧的字符,self.right存放光标右侧的字符。
- 使用两个列表模拟左右栈,其中
- addText:
- 将新增的文本直接追加到
self.left中,光标自动位于新增文本的末尾。
- 将新增的文本直接追加到
- deleteText:
- 从
self.left中删除最多 k 个字符,返回实际删除的数量。
- 从
- cursorLeft & cursorRight:
- 通过在两个栈之间转移字符实现光标移动,移动后返回光标左侧最多 10 个字符(保持原字符串顺序)。
这种解法可以模拟文本编辑器中光标的左右移动与文本修改,且符合题目的时间复杂度要求。