Skip to content

Commit 573bf16

Browse files
authored
Update 2296-design-a-text-editor.js
1 parent 5279809 commit 573bf16

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

2296-design-a-text-editor.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,76 @@
1+
const TextEditor = function () {
2+
this.stk1 = []
3+
this.stk2 = []
4+
}
5+
6+
/**
7+
* @param {string} text
8+
* @return {void}
9+
*/
10+
TextEditor.prototype.addText = function (text) {
11+
for(const ch of text) {
12+
this.stk1.push(ch)
13+
}
14+
}
15+
16+
/**
17+
* @param {number} k
18+
* @return {number}
19+
*/
20+
TextEditor.prototype.deleteText = function (k) {
21+
let res = 0
22+
while(this.stk1.length && k) {
23+
k--
24+
res++
25+
this.stk1.pop()
26+
}
27+
return res
28+
}
29+
30+
/**
31+
* @param {number} k
32+
* @return {string}
33+
*/
34+
TextEditor.prototype.cursorLeft = function (k) {
35+
let res = ''
36+
while(this.stk1.length && k) {
37+
const tmp = this.stk1.pop()
38+
this.stk2.push(tmp)
39+
k--
40+
}
41+
42+
43+
for(let len = this.stk1.length, size = Math.min(10, this.stk1.length), i = 0; i < size; i++) {
44+
res = this.stk1[len - 1 - i] + res
45+
}
46+
47+
48+
return res
49+
}
50+
51+
/**
52+
* @param {number} k
53+
* @return {string}
54+
*/
55+
TextEditor.prototype.cursorRight = function (k) {
56+
let res = ''
57+
58+
while(this.stk2.length && k) {
59+
const tmp = this.stk2.pop()
60+
this.stk1.push(tmp)
61+
k--
62+
}
63+
64+
for(let len = this.stk1.length, size = Math.min(10, this.stk1.length), i = 0; i < size; i++) {
65+
res = this.stk1[len - 1 - i] + res
66+
}
67+
68+
return res
69+
}
70+
71+
// another
72+
73+
174
class Node {
275
constructor(val) {
376
this.val = val

0 commit comments

Comments
 (0)