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 8f28dfb

Browse files
committed
feat: add solutions to lc problem: No.2296
No.2296.Design a Text Editor
1 parent d53d5de commit 8f28dfb

File tree

6 files changed

+456
-84
lines changed

6 files changed

+456
-84
lines changed

‎solution/2200-2299/2296.Design a Text Editor/README.md‎

Lines changed: 158 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ textEditor.cursorRight(6); // 返回 "practi"
8181

8282
<!-- 这里可写通用的实现逻辑 -->
8383

84-
**方法一:字符串/切片模拟**
84+
**方法一:左右栈**
85+
86+
我们可以使用两个栈 `left``right`,其中栈 `left` 存储光标左边的字符,另一个栈 `right` 存储光标右边的字符。
87+
88+
- 当调用 `addText` 方法时,我们将 `text` 中的字符依次入栈 `left`。时间复杂度 $O(|text|)$。
89+
- 当调用 `deleteText` 方法时,我们将 `left` 中的字符出栈最多 $k$ 次。时间复杂度 $O(k)$。
90+
- 当调用 `cursorLeft` 方法时,我们将 `left` 中的字符出栈最多 $k$ 次,然后将出栈的字符依次入栈 `right`,最后返回 `left` 栈最多 10ドル$ 个字符。时间复杂度 $O(k)$。
91+
- 当调用 `cursorRight` 方法时,我们将 `right` 中的字符出栈最多 $k$ 次,然后将出栈的字符依次入栈 `left`,最后返回 `left` 栈最多 10ドル$ 个字符。时间复杂度 $O(k)$。
8592

8693
<!-- tabs:start -->
8794

@@ -91,29 +98,31 @@ textEditor.cursorRight(6); // 返回 "practi"
9198

9299
```python
93100
class TextEditor:
101+
94102
def __init__(self):
95-
self.idx = 0
96-
self.s = []
103+
self.left = []
104+
self.right = []
97105

98106
def addText(self, text: str) -> None:
99-
t = list(text)
100-
self.s[self.idx : self.idx] = t
101-
self.idx += len(t)
107+
self.left.extend(list(text))
102108

103109
def deleteText(self, k: int) -> int:
104-
k = min(self.idx, k)
105-
self.s[self.idx - k : self.idx] = []
106-
self.idx -= k
110+
k = min(k, len(self.left))
111+
for _ inrange(k):
112+
self.left.pop()
107113
return k
108114

109115
def cursorLeft(self, k: int) -> str:
110-
self.idx = max(0, self.idx - k)
111-
return ''.join(self.s[max(0, self.idx - 10) : self.idx])
116+
k = min(k, len(self.left))
117+
for _ in range(k):
118+
self.right.append(self.left.pop())
119+
return ''.join(self.left[-10:])
112120

113121
def cursorRight(self, k: int) -> str:
114-
self.idx = min(len(self.s), self.idx + k)
115-
return ''.join(self.s[max(0, self.idx - 10) : self.idx])
116-
122+
k = min(k, len(self.right))
123+
for _ in range(k):
124+
self.left.append(self.right.pop())
125+
return ''.join(self.left[-10:])
117126

118127
# Your TextEditor object will be instantiated and called as such:
119128
# obj = TextEditor()
@@ -129,33 +138,39 @@ class TextEditor:
129138

130139
```java
131140
class TextEditor {
132-
private int idx = 0;
133-
private StringBuilder s = new StringBuilder();
141+
private StringBuilder left = newStringBuilder();
142+
private StringBuilder right = new StringBuilder();
134143

135144
public TextEditor() {
145+
136146
}
137147

138148
public void addText(String text) {
139-
s.insert(idx, text);
140-
idx += text.length();
149+
left.append(text);
141150
}
142151

143152
public int deleteText(int k) {
144-
k = Math.min(idx, k);
145-
for (int i = 0; i < k; ++i) {
146-
s.deleteCharAt(--idx);
147-
}
153+
k = Math.min(k, left.length());
154+
left.setLength(left.length() - k);
148155
return k;
149156
}
150157

151158
public String cursorLeft(int k) {
152-
idx = Math.max(0, idx - k);
153-
return s.substring(Math.max(0, idx - 10), idx);
159+
k = Math.min(k, left.length());
160+
for (int i = 0; i < k; ++i) {
161+
right.append(left.charAt(left.length() - 1));
162+
left.deleteCharAt(left.length() - 1);
163+
}
164+
return left.substring(Math.max(left.length() - 10, 0));
154165
}
155166

156167
public String cursorRight(int k) {
157-
idx = Math.min(s.length(), idx + k);
158-
return s.substring(Math.max(0, idx - 10), idx);
168+
k = Math.min(k, right.length());
169+
for (int i = 0; i < k; ++i) {
170+
left.append(right.charAt(right.length() - 1));
171+
right.deleteCharAt(right.length() - 1);
172+
}
173+
return left.substring(Math.max(left.length() - 10, 0));
159174
}
160175
}
161176

@@ -169,6 +184,123 @@ class TextEditor {
169184
*/
170185
```
171186

187+
### **C++**
188+
189+
```cpp
190+
class TextEditor {
191+
public:
192+
TextEditor() {
193+
}
194+
195+
void addText(string text) {
196+
left += text;
197+
}
198+
199+
int deleteText(int k) {
200+
k = min(k, (int) left.size());
201+
left.resize(left.size() - k);
202+
return k;
203+
}
204+
205+
string cursorLeft(int k) {
206+
k = min(k, (int) left.size());
207+
while (k--) {
208+
right += left.back();
209+
left.pop_back();
210+
}
211+
return left.substr(max(0, (int) left.size() - 10));
212+
}
213+
214+
string cursorRight(int k) {
215+
k = min(k, (int) right.size());
216+
while (k--) {
217+
left += right.back();
218+
right.pop_back();
219+
}
220+
return left.substr(max(0, (int) left.size() - 10));
221+
}
222+
223+
private:
224+
string left, right;
225+
};
226+
227+
/**
228+
* Your TextEditor object will be instantiated and called as such:
229+
* TextEditor* obj = new TextEditor();
230+
* obj->addText(text);
231+
* int param_2 = obj->deleteText(k);
232+
* string param_3 = obj->cursorLeft(k);
233+
* string param_4 = obj->cursorRight(k);
234+
*/
235+
```
236+
237+
### **Go**
238+
239+
```go
240+
type TextEditor struct {
241+
left, right []byte
242+
}
243+
244+
func Constructor() TextEditor {
245+
return TextEditor{}
246+
}
247+
248+
func (this *TextEditor) AddText(text string) {
249+
this.left = append(this.left, text...)
250+
}
251+
252+
func (this *TextEditor) DeleteText(k int) int {
253+
k = min(k, len(this.left))
254+
if k < len(this.left) {
255+
this.left = this.left[:len(this.left)-k]
256+
} else {
257+
this.left = []byte{}
258+
}
259+
return k
260+
}
261+
262+
func (this *TextEditor) CursorLeft(k int) string {
263+
k = min(k, len(this.left))
264+
for ; k > 0; k-- {
265+
this.right = append(this.right, this.left[len(this.left)-1])
266+
this.left = this.left[:len(this.left)-1]
267+
}
268+
return string(this.left[max(len(this.left)-10, 0):])
269+
}
270+
271+
func (this *TextEditor) CursorRight(k int) string {
272+
k = min(k, len(this.right))
273+
for ; k > 0; k-- {
274+
this.left = append(this.left, this.right[len(this.right)-1])
275+
this.right = this.right[:len(this.right)-1]
276+
}
277+
return string(this.left[max(len(this.left)-10, 0):])
278+
}
279+
280+
func max(a, b int) int {
281+
if a > b {
282+
return a
283+
}
284+
return b
285+
}
286+
287+
func min(a, b int) int {
288+
if a < b {
289+
return a
290+
}
291+
return b
292+
}
293+
294+
/**
295+
* Your TextEditor object will be instantiated and called as such:
296+
* obj := Constructor();
297+
* obj.AddText(text);
298+
* param_2 := obj.DeleteText(k);
299+
* param_3 := obj.CursorLeft(k);
300+
* param_4 := obj.CursorRight(k);
301+
*/
302+
```
303+
172304
### **TypeScript**
173305

174306
```ts

0 commit comments

Comments
(0)

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