-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2296.design-a-text-editor.cpp
More file actions
57 lines (50 loc) · 1.16 KB
/
2296.design-a-text-editor.cpp
File metadata and controls
57 lines (50 loc) · 1.16 KB
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
#
# @lc app=leetcode id=2296 lang=cpp
#
# [2296] Design a Text Editor
#
# @lc code=start
class TextEditor {
private:
string L,R;
public:
TextEditor() {
}
void addText(string text) {
L.append(text);
}
int deleteText(int k) {
int cnt = min(k,(int)L.size());
L.resize(L.size()-cnt);
return cnt;
}
string cursorLeft(int k) {
int mv = min(k,(int)L.size());
while(mv--) {
R.push_back(L.back());
L.pop_back();
}
int len = L.size();
int start = max(0,len-10);
return L.substr(start);
}
string cursorRight(int k) {
int mv = min(k,(int)R.size());
while(mv--) {
L.push_back(R.back());
R.pop_back();
}
int len = L.size();
int start = max(0,len-10);
return L.substr(start);
}
};
/**
* 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);
*/
# @lc code=end