-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
45 lines (39 loc) · 1002 Bytes
/
Solution.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
class TextEditor {
public:
TextEditor() {
}
void addText(string text) {
left += text;
}
int deleteText(int k) {
k = min(k, (int) left.size());
left.resize(left.size() - k);
return k;
}
string cursorLeft(int k) {
k = min(k, (int) left.size());
while (k--) {
right += left.back();
left.pop_back();
}
return left.substr(max(0, (int) left.size() - 10));
}
string cursorRight(int k) {
k = min(k, (int) right.size());
while (k--) {
left += right.back();
right.pop_back();
}
return left.substr(max(0, (int) left.size() - 10));
}
private:
string left, right;
};
/**
* 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);
*/