LeetCode 2296. Design a Text Editor

原题链接在这里:https://leetcode.com/problems/design-a-text-editor/

题目:

Design a text editor with a cursor that can do the following:

  • Add text to where the cursor is.
  • Delete text from where the cursor is (simulating the backspace key).
  • Move the cursor either left or right.

When deleting text, only characters to the left of the cursor will be deleted. The cursor will also remain within the actual text and cannot be moved beyond it. More formally, we have that 0 <= cursor.position <= currentText.length always holds.

Implement the TextEditor class:

  • TextEditor() Initializes the object with empty text.
  • void addText(string text) Appends text to where the cursor is. The cursor ends to the right of text.
  • int deleteText(int k) Deletes k characters to the left of the cursor. Returns the number of characters actually deleted.
  • string cursorLeft(int k) Moves the cursor to the left k times. Returns the last min(10, len) characters to the left of the cursor, where len is the number of characters to the left of the cursor.
  • string cursorRight(int k) Moves the cursor to the right k times. Returns the last min(10, len) characters to the left of the cursor, where len is the number of characters to the left of the cursor.

Example 1:

Input
["TextEditor", "addText", "deleteText", "addText", "cursorRight", "cursorLeft", "deleteText", "cursorLeft", "cursorRight"]
[[], ["leetcode"], [4], ["practice"], [3], [8], [10], [2], [6]]
Output
[null, null, 4, null, "etpractice", "leet", 4, "", "practi"]

Explanation
TextEditor textEditor = new TextEditor(); // The current text is "|". (The '|' character represents the cursor)
textEditor.addText("leetcode"); // The current text is "leetcode|".
textEditor.deleteText(4); // return 4
                          // The current text is "leet|". 
                          // 4 characters were deleted.
textEditor.addText("practice"); // The current text is "leetpractice|". 
textEditor.cursorRight(3); // return "etpractice"
                           // The current text is "leetpractice|". 
                           // The cursor cannot be moved beyond the actual text and thus did not move.
                           // "etpractice" is the last 10 characters to the left of the cursor.
textEditor.cursorLeft(8); // return "leet"
                          // The current text is "leet|practice".
                          // "leet" is the last min(10, 4) = 4 characters to the left of the cursor.
textEditor.deleteText(10); // return 4
                           // The current text is "|practice".
                           // Only 4 characters were deleted.
textEditor.cursorLeft(2); // return ""
                          // The current text is "|practice".
                          // The cursor cannot be moved beyond the actual text and thus did not move. 
                          // "" is the last min(10, 0) = 0 characters to the left of the cursor.
textEditor.cursorRight(6); // return "practi"
                           // The current text is "practi|ce".
                           // "practi" is the last min(10, 6) = 6 characters to the left of the cursor. 

Constraints:

  • 1 <= text.length, k <= 40
  • text consists of lowercase English letters.
  • At most 2 * 104 calls in total will be made to addTextdeleteTextcursorLeft and cursorRight.

Follow-up: Could you find a solution with time complexity of O(k) per call?

题解:

Use two stacks. One for curser left, one for curse right.

When move curser to left, pop left and push to right. Vice versa.

Time Complexity: addText, O(n). n = text.length(). deleteText, O(k). cursorLeft, O(k). cursorRight, O(k).

AC Java:

复制代码
 1 class TextEditor {
 2     Stack<Character> left;
 3     Stack<Character> right;
 4     
 5     public TextEditor() {
 6         left = new Stack<>();
 7         right = new Stack<>();
 8     }
 9     
10     public void addText(String text) {
11         for(char c : text.toCharArray()){
12             left.push(c);
13         }
14     }
15     
16     public int deleteText(int k) {
17         int count = 0;
18         while(!left.isEmpty() && k > 0){
19             left.pop();
20             k--;
21             count++;
22         }
23         
24         return count;
25     }
26     
27     public String cursorLeft(int k) {
28         while(!left.isEmpty() && k > 0){
29             right.push(left.pop());
30             k--;
31         }
32         
33         return lastTen();
34     }
35     
36     public String cursorRight(int k) {
37         while(!right.isEmpty() && k > 0){
38             left.push(right.pop());
39             k--;
40         }
41         
42         return lastTen();
43     }
44     
45     private String lastTen(){
46         StringBuilder sb = new StringBuilder();
47         int count = 10;
48         while(!left.isEmpty() && count > 0){
49             sb.append(left.pop());
50             count--;
51         }
52         
53         String res = sb.reverse().toString();
54         addText(res);
55         return res;
56     }
57 }
58 
59 /**
60  * Your TextEditor object will be instantiated and called as such:
61  * TextEditor obj = new TextEditor();
62  * obj.addText(text);
63  * int param_2 = obj.deleteText(k);
64  * String param_3 = obj.cursorLeft(k);
65  * String param_4 = obj.cursorRight(k);
66  */
复制代码

 

posted @   Dylan_Java_NYC  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· Supergateway:MCP服务器的远程调试与集成工具
· C# 13 中的新增功能实操
历史上的今天:
2017-09-29 LeetCode 486. Predict the Winner
2017-09-29 LeetCode 416. Partition Equal Subset Sum
2015-09-29 LeetCode 216. Combination Sum III
点击右上角即可分享
微信分享提示