Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8789f5e

Browse files
Create 2296.Design-a-Text-Editor_v2.cpp
1 parent 44c7d53 commit 8789f5e

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
class TextEditor {
2+
stack<char>st1;
3+
stack<char>st2;
4+
public:
5+
TextEditor() {
6+
7+
}
8+
9+
void addText(string text)
10+
{
11+
for (auto ch: text)
12+
st1.push(ch);
13+
}
14+
15+
int deleteText(int k)
16+
{
17+
int ret = min(k, (int)st1.size());
18+
for (int i=0; i<ret; i++)
19+
{
20+
st1.pop();
21+
}
22+
return ret;
23+
24+
}
25+
26+
string cursorLeft(int k)
27+
{
28+
int t = min(k, (int)st1.size());
29+
for (int i=0; i<t; i++)
30+
{
31+
st2.push(st1.top());
32+
st1.pop();
33+
}
34+
t = min(10, (int)st1.size());
35+
string ret;
36+
for (int i=0; i<t; i++)
37+
{
38+
ret.push_back(st1.top());
39+
st1.pop();
40+
}
41+
reverse(ret.begin(), ret.end());
42+
for (int i=0; i<ret.size(); i++)
43+
st1.push(ret[i]);
44+
return ret;
45+
}
46+
47+
string cursorRight(int k)
48+
{
49+
int t = min(k, (int)st2.size());
50+
for (int i=0; i<t; i++)
51+
{
52+
st1.push(st2.top());
53+
st2.pop();
54+
}
55+
t = min(10, (int)st1.size());
56+
string ret;
57+
for (int i=0; i<t; i++)
58+
{
59+
ret.push_back(st1.top());
60+
st1.pop();
61+
}
62+
reverse(ret.begin(), ret.end());
63+
for (int i=0; i<ret.size(); i++)
64+
st1.push(ret[i]);
65+
return ret;
66+
}
67+
};
68+
69+
/**
70+
* Your TextEditor object will be instantiated and called as such:
71+
* TextEditor* obj = new TextEditor();
72+
* obj->addText(text);
73+
* int param_2 = obj->deleteText(k);
74+
* string param_3 = obj->cursorLeft(k);
75+
* string param_4 = obj->cursorRight(k);
76+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /