@@ -81,7 +81,14 @@ textEditor.cursorRight(6); // 返回 "practi"
81
81
82
82
<!-- 这里可写通用的实现逻辑 -->
83
83
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)$。
85
92
86
93
<!-- tabs:start -->
87
94
@@ -91,29 +98,31 @@ textEditor.cursorRight(6); // 返回 "practi"
91
98
92
99
``` python
93
100
class TextEditor :
101
+
94
102
def __init__ (self ):
95
- self .idx = 0
96
- self .s = []
103
+ self .left = []
104
+ self .right = []
97
105
98
106
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))
102
108
103
109
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 _ in range (k):
112
+ self .left.pop()
107
113
return k
108
114
109
115
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 :])
112
120
113
121
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 :])
117
126
118
127
# Your TextEditor object will be instantiated and called as such:
119
128
# obj = TextEditor()
@@ -129,33 +138,39 @@ class TextEditor:
129
138
130
139
``` java
131
140
class TextEditor {
132
- private int idx = 0 ;
133
- private StringBuilder s = new StringBuilder ();
141
+ private StringBuilder left = new StringBuilder () ;
142
+ private StringBuilder right = new StringBuilder ();
134
143
135
144
public TextEditor () {
145
+
136
146
}
137
147
138
148
public void addText (String text ) {
139
- s. insert(idx, text);
140
- idx += text. length();
149
+ left. append(text);
141
150
}
142
151
143
152
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);
148
155
return k;
149
156
}
150
157
151
158
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 ));
154
165
}
155
166
156
167
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 ));
159
174
}
160
175
}
161
176
@@ -169,6 +184,123 @@ class TextEditor {
169
184
*/
170
185
```
171
186
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
+
172
304
### ** TypeScript**
173
305
174
306
``` ts
0 commit comments