forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
40 lines (33 loc) · 943 Bytes
/
Solution.java
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
class TextEditor {
private int idx = 0;
private StringBuilder s = new StringBuilder();
public TextEditor() {
}
public void addText(String text) {
s.insert(idx, text);
idx += text.length();
}
public int deleteText(int k) {
k = Math.min(idx, k);
for (int i = 0; i < k; ++i) {
s.deleteCharAt(--idx);
}
return k;
}
public String cursorLeft(int k) {
idx = Math.max(0, idx - k);
return s.substring(Math.max(0, idx - 10), idx);
}
public String cursorRight(int k) {
idx = Math.min(s.length(), idx + k);
return s.substring(Math.max(0, idx - 10), idx);
}
}
/**
* 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);
*/