-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesignATextEditor.cpp
67 lines (56 loc) · 1.39 KB
/
DesignATextEditor.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class TextEditor {
public:
string text;
int cursor;
TextEditor() {
text = "";
cursor = 0;
}
void addText(string text) {
this->text.insert(cursor, text);
cursor += text.length();
}
int deleteText(int k) {
int k_del = 0;
if (cursor >= k) {
text.erase(cursor-k, k);
cursor -= k;
return k;
} else {
text.erase(0, cursor);
k_del = cursor;
cursor = 0;
return k_del;
}
}
string cursorLeft(int k) {
cursor -= k;
if (cursor < 0) {
cursor = 0;
}
if (cursor >= 10) {
return text.substr(cursor-10, 10);
} else {
return text.substr(0, cursor);
}
}
string cursorRight(int k) {
cursor += k;
if (cursor > text.length()) {
cursor = text.length();
}
if (cursor >= 10) {
return text.substr(cursor-10, 10);
} else {
return text.substr(0, cursor);
}
}
};
/**
* 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);
*/