Solution
设计一个文本编辑器
考试时用stl写了个模拟过了,后来加强了数据被卡掉了。正确做法应该是利用双链表或者对顶栈,这种构造还是比较巧妙的。
Code
class TextEditor {
public:
vector<char> left, right;
TextEditor() {
}
void addText(string text) {
left.insert(left.end(), text.begin(), text.end());
}
int deleteText(int k) {
int res = 0;
while (k && left.size()) {
left.pop_back();
k--;
res++;
}
return res;
}
string cursorLeft(int k) {
while (k && left.size()) {
right.push_back(left.back());
left.pop_back();
k--;
}
return string(left.begin() + max((int)left.size() - 10, 0), left.end());
}
string cursorRight(int k) {
while (k && right.size()) {
left.push_back(right.back());
right.pop_back();
k--;
}
return string(left.begin() + max((int)left.size() - 10, 0), left.end());
}
};
/**
* 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);
*/
- Post link: https://scnujackychen.github.io/2022/06/16/LC-weekly-contest-296/
- Copyright Notice: All articles in this blog are licensed under unless otherwise stated.
若没有本文 Issue,您可以使用 Comment 模版新建。
GitHub IssuesGitHub Discussions