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


    比较暴力的解法,使用StringBuilder来模拟字符串的变化,完全数学化去理解,但性能会差一些,在题解区看到了有人提到了对顶栈。看了几篇介绍帖子,试着写了下,确实写起来更简单。
    参考链接:对顶栈图解

    class TextEditor {
            /**
             * 使用对顶栈解决字符移动问题:
             * 一左一右两个栈left、right相对,添加时将字符串中字符都从队尾加入left栈
             * 删除时,将left栈中的字符从队尾移除Math.min(left.size(), k)个字符
             * 左移时,将需要移除的字符(数量为Math.min(left.size(), k))从left栈队尾取出并从队头放入right栈
             * 右移时,将需要移除的字符(数量为Math.min(right.size(), k))从right栈队头取出并从队位放入left栈
             */
            private LinkedList<Character> left = null;
            private LinkedList<Character> right = null;
    
            /**
             * 初始化
             */
            public TextEditor() {
                left = new LinkedList<>();
                right = new LinkedList<>();
            }
    
            /**
             * 将 text中字符逐个从left栈队尾放入
             * @param text 要入left栈的字符串
             */
            public void addText(String text) {
                for (Character c : text.toCharArray()){
                    left.addLast(c);
                }
            }
    
            /**
             * 删除光标左边 k 个字符。返回实际删除的字符数目。
             * 将left栈中的字符从队尾移除Math.min(left.size(), k)个字符
             * @param k 输入数字k
             * @return 实际删除的字符数量
             */
            public int deleteText(int k) {
                int count = Math.min(left.size(), k);
                for (int i = 0; i < count; i++) {
                    left.removeLast();
                }
                return count;
            }
    
            /**
             * 将光标向左移动 k 次。返回移动后光标左边 min(10, len) 个字符,其中 len 是光标左边的 字符数目。
             * 左移时,将需要移动的字符(数量为Math.min(left.size(), k))从left栈队尾取出并从队头放入right栈
             * @param k 输入数字k
             * @return  返回移动字符组成的字符串
             */
            public String cursorLeft(int k) {
                int count = Math.min(left.size(), k);
                for (int i = 0; i < count; i++) {
                    Character character = left.removeLast();
                    right.addFirst(character);
                }
                return getLeftKCharacters(k);
            }
    
            /**
             * 将光标向右边移动 k 次。返回移动后光标左边 min(10, len) 个字符,其中 len 是光标左边的 字符数目。
             *
             * @param k 输入数字k
             * @return  返回移动字符组成的字符串
             */
            public String cursorRight(int k) {
                int count = Math.min(right.size(), k);
                for (int i = 0; i < count; i++) {
                    Character character = right.removeFirst();
                    left.addLast(character);
                }
                return getLeftKCharacters(k);
            }
    
            /**
             * 从队尾逐个获取left栈中的Math.min(left.size(),k)个字符
             * @param k 输入数字k
             * @return  返回取出字符组成的字符串
             */
            public String getLeftKCharacters(int k) {
                int len = Math.max(left.size() - 10,0);
                StringBuilder str = new StringBuilder();
                for (int i = len; i < left.size() ; i++) {
                    str.append(left.get(i));
                }
                return str.toString();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
  • 相关阅读:
    飞书开发学习笔记(七)-添加机器人及发送webhook消息
    LeetCode_专项突破之SQL入门
    Python潮流周刊#9:如何在本地部署开源大语言模型?
    批量提取文件名到excel?
    window环境下安装大数据环境
    【精华】AI Agent:大模型改变世界的“钥匙”
    Vue-Router4 学习记录
    Netty进阶——粘包与半包(滑动窗口)
    【深度学习】基于tensorflow的服装图像分类训练(数据集:Fashion-MNIST)
    基于 51 的点阵屏显示 8*8 点阵仿真实验
  • 原文地址:https://blog.csdn.net/Mr_Richard/article/details/133246126