LeetCode 2296 设计一个文本编辑器

自然而然的想到使用双向链表
在这里插入图片描述
初始化一个head和tail,构建成双向链表。point表示当前光标的位置,leftCount和rightCount分别表示point离head和tail的距离。cursorLeft函数和cursorRight函数需要根据这个距离做不同的处理。增加文本和删除文本都需要修改leftCount的值,而移动光标(cursorLeft函数和cursorRight函数)需要修改leftCount和rightCount的值。
直接看代码吧,逻辑不复杂。

class TextEditor {

	Node head;
	Node tail;
	Node point;
	int leftCount = 0;
	int rightCount = 0;
    public TextEditor() {
    	head = new Node();
    	tail = new Node();
    	point = head;
    	head.next = tail;
    	tail.pre = head;
    }
    
	public void addText(String text) {
        Node next = point.next;
        for(int i = 0; i < text.length(); i++) {
            char charAt = text.charAt(i);
            Node node = new Node();
            node.c = charAt;
            point.next = node;
            node.pre = point;
            point = node;
            leftCount++;
        }
        point.next = next;
        next.pre = point;
    }

    public int deleteText(int k) {
        if(k > leftCount){
            head.next = point.next;
            point = head;
            int temp = leftCount;
            leftCount = 0;
            return temp;
        }
        Node tempNode = point.next;
        for(int i = 0; i < k; i++) {
            point = point.pre;
        }
        point.next = tempNode;
        tempNode.pre = point;
        leftCount -= k;
        return k;
    }

    public String cursorLeft(int k) {
        if(k > leftCount) {
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < leftCount; i++) {
                point = point.pre;
            }
            rightCount += leftCount;
            leftCount = 0;
            return getString();
        }
        for(int i = 0; i < k; i++) {
            point = point.pre;
        }
        leftCount -= k;
        rightCount += k;
        return getString();
    }

    public String cursorRight(int k) {
        if(k > rightCount) {
            for(int i = 0; i < rightCount; i++) {
                point = point.next;
            }
            leftCount += rightCount;
            rightCount = 0;
            return getString();
        }
        for(int i = 0; i < k; i++) {
            point = point.next;
        }
        rightCount -= k;
        leftCount += k;
        return getString();
    }

    private String getString(){
        StringBuilder sb = new StringBuilder();
        Node tempNode = point;
        int temp = Math.min(10, leftCount);
        for(int i = 0; i < temp; i++) {
            sb.append(tempNode.c);
            tempNode = tempNode.pre;
        }
        return sb.reverse().toString();
    }
}

class Node {
	Character c;
	Node pre;
	Node next;
}

结果,中规中矩的结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值