Skip to content

Commit bd78e50

Browse files
authored
Create 2296-design-a-text-editor.js
1 parent 67e3454 commit bd78e50

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
@@ -0,0 +1,73 @@
1+
class Node {
2+
constructor(val) {
3+
this.val = val
4+
this.prev = null
5+
this.next = null
6+
}
7+
}
8+
9+
var TextEditor = function() {
10+
this.left = []
11+
this.right = []
12+
this.idx = 0
13+
};
14+
15+
/**
16+
* @param {string} text
17+
* @return {void}
18+
*/
19+
TextEditor.prototype.addText = function(text) {
20+
for(const ch of text) this.left.push(ch)
21+
};
22+
23+
/**
24+
* @param {number} k
25+
* @return {number}
26+
*/
27+
TextEditor.prototype.deleteText = function(k) {
28+
let res = 0
29+
while(this.left.length && k) {
30+
res++
31+
this.left.pop()
32+
k--
33+
}
34+
return res
35+
};
36+
37+
/**
38+
* @param {number} k
39+
* @return {string}
40+
*/
41+
TextEditor.prototype.cursorLeft = function(k) {
42+
while(k && this.left.length) {
43+
const tmp = this.left.pop()
44+
this.right.push(tmp)
45+
k--
46+
}
47+
48+
return this.left.slice(Math.max(0, this.left.length - 10), this.left.length).join('')
49+
};
50+
51+
/**
52+
* @param {number} k
53+
* @return {string}
54+
*/
55+
TextEditor.prototype.cursorRight = function(k) {
56+
while(k && this.right.length) {
57+
const tmp = this.right.pop()
58+
this.left.push(tmp)
59+
k--
60+
}
61+
62+
return this.left.slice(Math.max(0, this.left.length - 10), this.left.length).join('')
63+
};
64+
65+
66+
/**
67+
* Your TextEditor object will be instantiated and called as such:
68+
* var obj = new TextEditor()
69+
* obj.addText(text)
70+
* var param_2 = obj.deleteText(k)
71+
* var param_3 = obj.cursorLeft(k)
72+
* var param_4 = obj.cursorRight(k)
73+
*/

0 commit comments

Comments
 (0)