diff --git a/solution/2200-2299/2295.Replace Elements in an Array/README_EN.md b/solution/2200-2299/2295.Replace Elements in an Array/README_EN.md index 284387d56548a..8ed95085bc8b5 100644 --- a/solution/2200-2299/2295.Replace Elements in an Array/README_EN.md +++ b/solution/2200-2299/2295.Replace Elements in an Array/README_EN.md @@ -76,7 +76,13 @@ We return the array [2,1]. -### 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. diff --git a/solution/2200-2299/2296.Design a Text Editor/README.md b/solution/2200-2299/2296.Design a Text Editor/README.md index 6d8b4274f4458..7797eda5351a2 100644 --- a/solution/2200-2299/2296.Design a Text Editor/README.md +++ b/solution/2200-2299/2296.Design a Text Editor/README.md @@ -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) + */ +``` + diff --git a/solution/2200-2299/2296.Design a Text Editor/README_EN.md b/solution/2200-2299/2296.Design a Text Editor/README_EN.md index c87145f63d889..18a663629271f 100644 --- a/solution/2200-2299/2296.Design a Text Editor/README_EN.md +++ b/solution/2200-2299/2296.Design a Text Editor/README_EN.md @@ -97,7 +97,14 @@ textEditor.cursorRight(6); // return "practi" -### 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)$. @@ -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) + */ +``` + diff --git a/solution/2200-2299/2296.Design a Text Editor/Solution.ts b/solution/2200-2299/2296.Design a Text Editor/Solution.ts new file mode 100644 index 0000000000000..cf402773de322 --- /dev/null +++ b/solution/2200-2299/2296.Design a Text Editor/Solution.ts @@ -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) + */