首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >LeetCode 6093. 设计一个文本编辑器(双栈)

LeetCode 6093. 设计一个文本编辑器(双栈)

作者头像
Michael阿明
发布2022-06-06 09:22:36
发布2022-06-06 09:22:36
6060
举报

文章目录

1. 题目

请你设计一个带光标的文本编辑器,它可以实现以下功能:

  • 添加:在光标所在处添加文本。
  • 删除:在光标所在处删除文本(模拟键盘的删除键)。
  • 移动:将光标往左或者往右移动。

当删除文本时,只有光标左边的字符会被删除。 光标会留在文本内,也就是说任意时候 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 是光标左边的字符数目。
代码语言:javascript
复制
示例 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 <= 40
text 只含有小写英文字母。
调用 addText ,deleteText ,cursorLeft 和 cursorRight 的 总 次数不超过 2 * 10^4 次。

来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/design-a-text-editor 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 使用 python 的 list 模拟
  • l, r 分别存储光标左侧 和 右侧 的字符
代码语言:javascript
复制
class TextEditor:

    def __init__(self):
        self.l = []
        self.r = []

    def addText(self, text: str) -> None:
        for t in text:
            self.l.append(t)

    def deleteText(self, k: int) -> int:
        ct = min(k, len(self.l))
        for i in range(ct):
            self.l.pop()
        return ct

    def cursorLeft(self, k: int) -> str:
        ct = min(k, len(self.l))
        for i in range(ct):
            self.r.append(self.l.pop())
        return ''.join(self.l[-min(len(self.l), 10):])

    def cursorRight(self, k: int) -> str:
        ct = min(k, len(self.r))
        for i in range(ct):
            self.l.append(self.r.pop())
        return ''.join(self.l[-min(len(self.l), 10):])

456 ms 38.3 MB Python3

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 题目
  • 2. 解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档