LeetCode 2296. 设计一个文本编辑器

2296. 设计一个文本编辑器

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

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

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

提示 1

Making changes in the middle of some data structures is generally harder than changing the front/back of the same data structure.


提示 2

Can you partition your data structure (text with cursor) into two parts, such that each part changes only near its ends?


提示 3

Can you think of a data structure that supports efficient removals/additions to the front/back?


提示 4

Try to solve the problem with two deques by maintaining the prefix and the suffix separately.

解法1:对顶栈 (双端队列/栈 + 前缀后缀)

算法逻辑

  • 使用两个双端队列 prefix 和 suffix 来存储文本,其中 prefix 存储从文本开头到光标左侧的字符,suffix 存储从光标右侧到文本末尾的字符。
  • 光标的位置由 prefix 的大小来隐式表示,即光标位于 prefix 的末尾。
  • 添加文本时,直接将字符添加到 prefix 的末尾,并更新光标位置。
  • 删除文本时,从 prefix 的末尾开始删除字符,直到删除了 k 个字符或 prefix 为空。
  • 光标左移时,将 prefix 的末尾字符移动到 suffix 的开头,直到移动了 k 次或 prefix 为空。
  • 光标右移时,将 suffix 的开头字符移动到 prefix 的末尾,直到移动了 k 次或 suffix 为空。

Java版:

class TextEditor {
    private List<Character> prefix;
    private List<Character> suffix;

    public TextEditor() {
        this.prefix = new LinkedList<>();
        this.suffix = new LinkedList<>();
    }
    
    public void addText(String text) {
        for (int i = 0; i < text.length(); i++) {
            prefix.add(text.charAt(i));
        }
    }
    
    public int deleteText(int k) {
        int ret = Math.min(k, prefix.size());
        while (!prefix.isEmpty() && k > 0) {
            k--;
            prefix.removeLast();
        }
        return ret;
    }
    
    public String cursorLeft(int k) {
        while (!prefix.isEmpty() && k > 0) {
            k--;
            suffix.add(prefix.removeLast());
        }
        int ret = Math.min(10, prefix.size());
        StringBuffer st = new StringBuffer();
        for (int i = prefix.size() - ret; i < prefix.size(); i++) {
            st.append(prefix.get(i));
        }
        return st.toString();
    }
    
    public String cursorRight(int k) {
        while (!suffix.isEmpty() && k > 0) {
            k--;
            prefix.add(suffix.removeLast());
        }
        int ret = Math.min(10, prefix.size());
        StringBuffer st = new StringBuffer();
        for (int i = prefix.size() - ret; i < prefix.size(); i++) {
            st.append(prefix.get(i));
        }
        return st.toString();
    }
}

/**
 * Your TextEditor object will be instantiated and called as such:
 * TextEditor obj = new TextEditor();
 * obj.addText(text);
 * int param_2 = obj.deleteText(k);
 * String param_3 = obj.cursorLeft(k);
 * String param_4 = obj.cursorRight(k);
 */

Python3版:

class TextEditor:

    def __init__(self):
        self.prefix = []
        self.suffix = []

    def addText(self, text: str) -> None:
        self.prefix.extend(list(text))

    def deleteText(self, k: int) -> int:
        ret = min(k, len(self.prefix))
        self.prefix = self.prefix[:len(self.prefix) - ret]
        return ret

    def cursorLeft(self, k: int) -> str:
        while self.prefix and k > 0:
            k -= 1
            self.suffix.append(self.prefix.pop())
        return ''.join(self.prefix[-10:])

    def cursorRight(self, k: int) -> str:
        while self.suffix and k > 0:
            k -= 1
            self.prefix.append(self.suffix.pop())
        return ''.join(self.prefix[-10:])

# Your TextEditor object will be instantiated and called as such:
# obj = TextEditor()
# obj.addText(text)
# param_2 = obj.deleteText(k)
# param_3 = obj.cursorLeft(k)
# param_4 = obj.cursorRight(k)

复杂度分析

  • 时空复杂度均与输入量成正比(线性)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值