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 f5031fc

Browse files
feat: add solutions to lc problem: No.2296 (doocs#3190)
1 parent 21b6ce8 commit f5031fc

File tree

4 files changed

+163
-2
lines changed

4 files changed

+163
-2
lines changed

‎solution/2200-2299/2295.Replace Elements in an Array/README_EN.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ We return the array [2,1].
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Hash Table
80+
81+
First, we use a hash table $d$ to record the index of each number in the array `nums`. Then, we iterate through the operation array `operations`. For each operation $[a, b],ドル we replace the number at index $d[a]$ in `nums` with $b,ドル and update the index of $b$ in $d$ to $d[a]$.
82+
83+
Finally, we return `nums`.
84+
85+
The time complexity is $O(n + m),ドル and the space complexity is $O(n)$. Here, $n$ and $m$ are the lengths of the arrays `nums` and `operations`, respectively.
8086

8187
<!-- tabs:start -->
8288

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,57 @@ func (this *TextEditor) CursorRight(k int) string {
301301
*/
302302
```
303303

304+
#### TypeScript
305+
306+
```ts
307+
class TextEditor {
308+
private left: string[];
309+
private right: string[];
310+
311+
constructor() {
312+
this.left = [];
313+
this.right = [];
314+
}
315+
316+
addText(text: string): void {
317+
this.left.push(...text);
318+
}
319+
320+
deleteText(k: number): number {
321+
k = Math.min(k, this.left.length);
322+
for (let i = 0; i < k; i++) {
323+
this.left.pop();
324+
}
325+
return k;
326+
}
327+
328+
cursorLeft(k: number): string {
329+
k = Math.min(k, this.left.length);
330+
for (let i = 0; i < k; i++) {
331+
this.right.push(this.left.pop()!);
332+
}
333+
return this.left.slice(-10).join('');
334+
}
335+
336+
cursorRight(k: number): string {
337+
k = Math.min(k, this.right.length);
338+
for (let i = 0; i < k; i++) {
339+
this.left.push(this.right.pop()!);
340+
}
341+
return this.left.slice(-10).join('');
342+
}
343+
}
344+
345+
/**
346+
* Your TextEditor object will be instantiated and called as such:
347+
* var obj = new TextEditor()
348+
* obj.addText(text)
349+
* var param_2 = obj.deleteText(k)
350+
* var param_3 = obj.cursorLeft(k)
351+
* var param_4 = obj.cursorRight(k)
352+
*/
353+
```
354+
304355
<!-- tabs:end -->
305356

306357
<!-- solution:end -->

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ textEditor.cursorRight(6); // return &quot;practi&quot;
9797

9898
<!-- solution:start -->
9999

100-
### Solution 1
100+
### Solution 1: Left and Right Stacks
101+
102+
We can use two stacks, `left` and `right`, where the `left` stack stores the characters to the left of the cursor, and the `right` stack stores the characters to the right of the cursor.
103+
104+
- When the `addText` method is called, we push the characters from `text` onto the `left` stack one by one. The time complexity is $O(|\text{text}|)$.
105+
- When the `deleteText` method is called, we pop characters from the `left` stack up to $k$ times. The time complexity is $O(k)$.
106+
- When the `cursorLeft` method is called, we pop characters from the `left` stack up to $k$ times, then push the popped characters onto the `right` stack, and finally return up to 10ドル$ characters from the `left` stack. The time complexity is $O(k)$.
107+
- When the `cursorRight` method is called, we pop characters from the `right` stack up to $k$ times, then push the popped characters onto the `left` stack, and finally return up to 10ドル$ characters from the `left` stack. The time complexity is $O(k)$.
101108

102109
<!-- tabs:start -->
103110

@@ -291,6 +298,57 @@ func (this *TextEditor) CursorRight(k int) string {
291298
*/
292299
```
293300

301+
#### TypeScript
302+
303+
```ts
304+
class TextEditor {
305+
private left: string[];
306+
private right: string[];
307+
308+
constructor() {
309+
this.left = [];
310+
this.right = [];
311+
}
312+
313+
addText(text: string): void {
314+
this.left.push(...text);
315+
}
316+
317+
deleteText(k: number): number {
318+
k = Math.min(k, this.left.length);
319+
for (let i = 0; i < k; i++) {
320+
this.left.pop();
321+
}
322+
return k;
323+
}
324+
325+
cursorLeft(k: number): string {
326+
k = Math.min(k, this.left.length);
327+
for (let i = 0; i < k; i++) {
328+
this.right.push(this.left.pop()!);
329+
}
330+
return this.left.slice(-10).join('');
331+
}
332+
333+
cursorRight(k: number): string {
334+
k = Math.min(k, this.right.length);
335+
for (let i = 0; i < k; i++) {
336+
this.left.push(this.right.pop()!);
337+
}
338+
return this.left.slice(-10).join('');
339+
}
340+
}
341+
342+
/**
343+
* Your TextEditor object will be instantiated and called as such:
344+
* var obj = new TextEditor()
345+
* obj.addText(text)
346+
* var param_2 = obj.deleteText(k)
347+
* var param_3 = obj.cursorLeft(k)
348+
* var param_4 = obj.cursorRight(k)
349+
*/
350+
```
351+
294352
<!-- tabs:end -->
295353

296354
<!-- solution:end -->
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class TextEditor {
2+
private left: string[];
3+
private right: string[];
4+
5+
constructor() {
6+
this.left = [];
7+
this.right = [];
8+
}
9+
10+
addText(text: string): void {
11+
this.left.push(...text);
12+
}
13+
14+
deleteText(k: number): number {
15+
k = Math.min(k, this.left.length);
16+
for (let i = 0; i < k; i++) {
17+
this.left.pop();
18+
}
19+
return k;
20+
}
21+
22+
cursorLeft(k: number): string {
23+
k = Math.min(k, this.left.length);
24+
for (let i = 0; i < k; i++) {
25+
this.right.push(this.left.pop()!);
26+
}
27+
return this.left.slice(-10).join('');
28+
}
29+
30+
cursorRight(k: number): string {
31+
k = Math.min(k, this.right.length);
32+
for (let i = 0; i < k; i++) {
33+
this.left.push(this.right.pop()!);
34+
}
35+
return this.left.slice(-10).join('');
36+
}
37+
}
38+
39+
/**
40+
* Your TextEditor object will be instantiated and called as such:
41+
* var obj = new TextEditor()
42+
* obj.addText(text)
43+
* var param_2 = obj.deleteText(k)
44+
* var param_3 = obj.cursorLeft(k)
45+
* var param_4 = obj.cursorRight(k)
46+
*/

0 commit comments

Comments
(0)

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