forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
48 lines (42 loc) · 1.12 KB
/
Solution.go
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
type TextEditor struct {
left, right []byte
}
func Constructor() TextEditor {
return TextEditor{}
}
func (this *TextEditor) AddText(text string) {
this.left = append(this.left, text...)
}
func (this *TextEditor) DeleteText(k int) int {
k = min(k, len(this.left))
if k < len(this.left) {
this.left = this.left[:len(this.left)-k]
} else {
this.left = []byte{}
}
return k
}
func (this *TextEditor) CursorLeft(k int) string {
k = min(k, len(this.left))
for ; k > 0; k-- {
this.right = append(this.right, this.left[len(this.left)-1])
this.left = this.left[:len(this.left)-1]
}
return string(this.left[max(len(this.left)-10, 0):])
}
func (this *TextEditor) CursorRight(k int) string {
k = min(k, len(this.right))
for ; k > 0; k-- {
this.left = append(this.left, this.right[len(this.right)-1])
this.right = this.right[:len(this.right)-1]
}
return string(this.left[max(len(this.left)-10, 0):])
}
/**
* Your TextEditor object will be instantiated and called as such:
* obj := Constructor();
* obj.AddText(text);
* param_2 := obj.DeleteText(k);
* param_3 := obj.CursorLeft(k);
* param_4 := obj.CursorRight(k);
*/