forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
46 lines (40 loc) · 1.07 KB
/
Solution.ts
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
class TextEditor {
private left: string[];
private right: string[];
constructor() {
this.left = [];
this.right = [];
}
addText(text: string): void {
this.left.push(...text);
}
deleteText(k: number): number {
k = Math.min(k, this.left.length);
for (let i = 0; i < k; i++) {
this.left.pop();
}
return k;
}
cursorLeft(k: number): string {
k = Math.min(k, this.left.length);
for (let i = 0; i < k; i++) {
this.right.push(this.left.pop()!);
}
return this.left.slice(-10).join('');
}
cursorRight(k: number): string {
k = Math.min(k, this.right.length);
for (let i = 0; i < k; i++) {
this.left.push(this.right.pop()!);
}
return this.left.slice(-10).join('');
}
}
/**
* Your TextEditor object will be instantiated and called as such:
* var obj = new TextEditor()
* obj.addText(text)
* var param_2 = obj.deleteText(k)
* var param_3 = obj.cursorLeft(k)
* var param_4 = obj.cursorRight(k)
*/