File tree 1 file changed +6
-6
lines changed
scripts/algorithms/D/Design a Text Editor
1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 380 ms (Top 41.73%) | Memory: 138.9 MB (Top 59.95%)
1
2
class TextEditor {
2
3
StringBuilder res ;
3
4
int pos =0 ;
4
-
5
+
5
6
public TextEditor () {
6
7
res = new StringBuilder ();
7
8
}
8
-
9
+
9
10
public void addText (String text ) {
10
11
res .insert (pos ,text );
11
12
pos += text .length ();
12
13
}
13
-
14
+
14
15
public int deleteText (int k ) {
15
16
int tmp = pos ;
16
17
pos -= k ;
17
18
if (pos <0 ) pos =0 ;
18
19
res .delete (pos ,tmp );
19
20
return tmp -pos ;
20
21
}
21
-
22
+
22
23
public String cursorLeft (int k ) {
23
24
pos -=k ;
24
25
if (pos <0 ) pos = 0 ;
25
26
if (pos <10 ) return res .substring (0 ,pos );
26
27
return res .substring (pos -10 ,pos );
27
28
}
28
-
29
+
29
30
public String cursorRight (int k ) {
30
31
pos +=k ;
31
32
if (pos >res .length ()) pos = res .length ();
32
33
if (pos <10 ) return res .substring (0 ,pos );
33
34
return res .substring (pos -10 ,pos );
34
35
}
35
36
}
36
-
You can’t perform that action at this time.
0 commit comments