-
Notifications
You must be signed in to change notification settings - Fork 3
/
2296.cpp
80 lines (76 loc) · 1.72 KB
/
2296.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
68
69
70
71
72
73
74
75
76
77
78
79
80
class TextEditor {
private:
list<char> txt;
list<char>::iterator cursor;
public:
TextEditor() {
cursor = txt.begin();
}
void addText(string text) {
for (auto c : text) {
cursor = txt.insert(cursor, c);
cursor++;
}
}
int deleteText(int k) {
if (cursor == txt.begin()) {
return 0;
}
cursor--;
int count = 0;
while (cursor != txt.begin() && k) {
cursor = txt.erase(cursor);
cursor--;
count++;
k--;
}
if (k) {
cursor = txt.erase(cursor);
count++;
k--;
}
else{
cursor++;
}
return count;
}
string _solve() {
list<char>::iterator cursorTemp = cursor;
int count = 10;
int out = 0;
while (cursorTemp != txt.begin() && count) {
cursorTemp--;
count--;
out++;
}
string res = "";
while (out) {
out--;
res.push_back(*cursorTemp);
cursorTemp++;
}
return res;
}
string cursorLeft(int k) {
while (cursor != txt.begin() && k) {
cursor--;
k--;
}
return _solve();
}
string cursorRight(int k) {
while (cursor != txt.end() && k) {
cursor++;
k--;
}
return _solve();
}
};
/**
* 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);
*/