Skip to content

feat: add solutions to lc problem: No.2296 #3190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ We return the array [2,1].

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table

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]$.

Finally, we return `nums`.

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.

<!-- tabs:start -->

Expand Down
51 changes: 51 additions & 0 deletions solution/2200-2299/2296.Design a Text Editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,57 @@ func (this *TextEditor) CursorRight(k int) string {
*/
```

#### TypeScript

```ts
class TextEditor {
private left: string[];
private right: string[];

constructor() {
this.left = [];
this.right = [];
}

addText(text: string): void {
this.left.push(...text);
}

deleteText(k: number): number {
k = Math.min(k, this.left.length);
for (let i = 0; i < k; i++) {
this.left.pop();
}
return k;
}

cursorLeft(k: number): string {
k = Math.min(k, this.left.length);
for (let i = 0; i < k; i++) {
this.right.push(this.left.pop()!);
}
return this.left.slice(-10).join('');
}

cursorRight(k: number): string {
k = Math.min(k, this.right.length);
for (let i = 0; i < k; i++) {
this.left.push(this.right.pop()!);
}
return this.left.slice(-10).join('');
}
}

/**
* Your TextEditor object will be instantiated and called as such:
* var obj = new TextEditor()
* obj.addText(text)
* var param_2 = obj.deleteText(k)
* var param_3 = obj.cursorLeft(k)
* var param_4 = obj.cursorRight(k)
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
60 changes: 59 additions & 1 deletion solution/2200-2299/2296.Design a Text Editor/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ textEditor.cursorRight(6); // return &quot;practi&quot;

<!-- solution:start -->

### Solution 1
### Solution 1: Left and Right Stacks

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.

- 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}|)$.
- When the `deleteText` method is called, we pop characters from the `left` stack up to $k$ times. The time complexity is $O(k)$.
- 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)$.
- 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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -291,6 +298,57 @@ func (this *TextEditor) CursorRight(k int) string {
*/
```

#### TypeScript

```ts
class TextEditor {
private left: string[];
private right: string[];

constructor() {
this.left = [];
this.right = [];
}

addText(text: string): void {
this.left.push(...text);
}

deleteText(k: number): number {
k = Math.min(k, this.left.length);
for (let i = 0; i < k; i++) {
this.left.pop();
}
return k;
}

cursorLeft(k: number): string {
k = Math.min(k, this.left.length);
for (let i = 0; i < k; i++) {
this.right.push(this.left.pop()!);
}
return this.left.slice(-10).join('');
}

cursorRight(k: number): string {
k = Math.min(k, this.right.length);
for (let i = 0; i < k; i++) {
this.left.push(this.right.pop()!);
}
return this.left.slice(-10).join('');
}
}

/**
* Your TextEditor object will be instantiated and called as such:
* var obj = new TextEditor()
* obj.addText(text)
* var param_2 = obj.deleteText(k)
* var param_3 = obj.cursorLeft(k)
* var param_4 = obj.cursorRight(k)
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
46 changes: 46 additions & 0 deletions solution/2200-2299/2296.Design a Text Editor/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class TextEditor {
private left: string[];
private right: string[];

constructor() {
this.left = [];
this.right = [];
}

addText(text: string): void {
this.left.push(...text);
}

deleteText(k: number): number {
k = Math.min(k, this.left.length);
for (let i = 0; i < k; i++) {
this.left.pop();
}
return k;
}

cursorLeft(k: number): string {
k = Math.min(k, this.left.length);
for (let i = 0; i < k; i++) {
this.right.push(this.left.pop()!);
}
return this.left.slice(-10).join('');
}

cursorRight(k: number): string {
k = Math.min(k, this.right.length);
for (let i = 0; i < k; i++) {
this.left.push(this.right.pop()!);
}
return this.left.slice(-10).join('');
}
}

/**
* Your TextEditor object will be instantiated and called as such:
* var obj = new TextEditor()
* obj.addText(text)
* var param_2 = obj.deleteText(k)
* var param_3 = obj.cursorLeft(k)
* var param_4 = obj.cursorRight(k)
*/
Loading