Skip to content
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

feat: update lc problems #4113

Merged
merged 1 commit into from
Feb 27, 2025
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
65 changes: 59 additions & 6 deletions solution/2200-2299/2296.Design a Text Editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ textEditor.deleteText(4); // 返回 4
// 删除了 4 个字符。
textEditor.addText("practice"); // 当前文本为 "leetpractice|" 。
textEditor.cursorRight(3); // 返回 "etpractice"
// 当前文本为 "leetpractice|".
// 当前文本为 "leetpractice|".
// 光标无法移动到文本以外,所以无法移动。
// "etpractice" 是光标左边的 10 个字符。
textEditor.cursorLeft(8); // 返回 "leet"
Expand Down Expand Up @@ -102,12 +102,12 @@ textEditor.cursorRight(6); // 返回 "practi"

### 方法一:左右栈

我们可以使用两个栈 `left``right`,其中栈 `left` 存储光标左边的字符,另一个栈 `right` 存储光标右边的字符。
我们可以使用两个栈 $\textit{left}$$\textit{right}$,其中栈 $\textit{left}$ 存储光标左边的字符,另一个栈 $\textit{right}$ 存储光标右边的字符。

- 当调用 `addText` 方法时,我们将 `text` 中的字符依次入栈 `left`。时间复杂度 $O(|text|)$。
- 当调用 `deleteText` 方法时,我们将 `left` 中的字符出栈最多 $k$ 次。时间复杂度 $O(k)$。
- 当调用 `cursorLeft` 方法时,我们将 `left` 中的字符出栈最多 $k$ 次,然后将出栈的字符依次入栈 `right`,最后返回 `left` 栈最多 $10$ 个字符。时间复杂度 $O(k)$。
- 当调用 `cursorRight` 方法时,我们将 `right` 中的字符出栈最多 $k$ 次,然后将出栈的字符依次入栈 `left`,最后返回 `left` 栈最多 $10$ 个字符。时间复杂度 $O(k)$。
- 当调用 $\text{addText}$ 方法时,我们将 $\text{text}$ 中的字符依次入栈 $\text{left}$。时间复杂度 $O(|\text{text}|)$。
- 当调用 $\text{deleteText}$ 方法时,我们将 $\text{left}$ 中的字符出栈最多 $k$ 次。时间复杂度 $O(k)$。
- 当调用 $\text{cursorLeft}$ 方法时,我们将 $\text{left}$ 中的字符出栈最多 $k$ 次,然后将出栈的字符依次入栈 $\text{right}$,最后返回 $\text{left}$ 栈最多 $10$ 个字符。时间复杂度 $O(k)$。
- 当调用 $\text{cursorRight}$ 方法时,我们将 $\text{right}$ 中的字符出栈最多 $k$ 次,然后将出栈的字符依次入栈 $\text{left}$,最后返回 $\text{left}$ 栈最多 $10$ 个字符。时间复杂度 $O(k)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -352,6 +352,59 @@ class TextEditor {
*/
```

#### Rust

```rust
struct TextEditor {
left: String,
right: String,
}

impl TextEditor {
fn new() -> Self {
TextEditor {
left: String::new(),
right: String::new(),
}
}

fn add_text(&mut self, text: String) {
self.left.push_str(&text);
}

fn delete_text(&mut self, k: i32) -> i32 {
let k = k.min(self.left.len() as i32) as usize;
self.left.truncate(self.left.len() - k);
k as i32
}

fn cursor_left(&mut self, k: i32) -> String {
let k = k.min(self.left.len() as i32) as usize;
for _ in 0..k {
if let Some(c) = self.left.pop() {
self.right.push(c);
}
}
self.get_last_10_chars()
}

fn cursor_right(&mut self, k: i32) -> String {
let k = k.min(self.right.len() as i32) as usize;
for _ in 0..k {
if let Some(c) = self.right.pop() {
self.left.push(c);
}
}
self.get_last_10_chars()
}

fn get_last_10_chars(&self) -> String {
let len = self.left.len();
self.left[len.saturating_sub(10)..].to_string()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
71 changes: 62 additions & 9 deletions solution/2200-2299/2296.Design a Text Editor/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ tags:
TextEditor textEditor = new TextEditor(); // The current text is &quot;|&quot;. (The &#39;|&#39; character represents the cursor)
textEditor.addText(&quot;leetcode&quot;); // The current text is &quot;leetcode|&quot;.
textEditor.deleteText(4); // return 4
// The current text is &quot;leet|&quot;.
// The current text is &quot;leet|&quot;.
// 4 characters were deleted.
textEditor.addText(&quot;practice&quot;); // The current text is &quot;leetpractice|&quot;.
textEditor.addText(&quot;practice&quot;); // The current text is &quot;leetpractice|&quot;.
textEditor.cursorRight(3); // return &quot;etpractice&quot;
// The current text is &quot;leetpractice|&quot;.
// The current text is &quot;leetpractice|&quot;.
// The cursor cannot be moved beyond the actual text and thus did not move.
// &quot;etpractice&quot; is the last 10 characters to the left of the cursor.
textEditor.cursorLeft(8); // return &quot;leet&quot;
Expand All @@ -72,7 +72,7 @@ textEditor.deleteText(10); // return 4
// Only 4 characters were deleted.
textEditor.cursorLeft(2); // return &quot;&quot;
// The current text is &quot;|practice&quot;.
// The cursor cannot be moved beyond the actual text and thus did not move.
// The cursor cannot be moved beyond the actual text and thus did not move.
// &quot;&quot; is the last min(10, 0) = 0 characters to the left of the cursor.
textEditor.cursorRight(6); // return &quot;practi&quot;
// The current text is &quot;practi|ce&quot;.
Expand All @@ -99,12 +99,12 @@ textEditor.cursorRight(6); // return &quot;practi&quot;

### 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.
We can use two stacks, $\textit{left}$ and $\textit{right}$, where the stack $\textit{left}$ stores the characters to the left of the cursor, and the stack $\textit{right}$ 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(|\textit{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)$.
- When calling the $\text{addText}$ method, we push the characters in $\text{text}$ onto the $\text{left}$ stack one by one. The time complexity is $O(|\text{text}|)$.
- When calling the $\text{deleteText}$ method, we pop characters from the $\text{left}$ stack up to $k$ times. The time complexity is $O(k)$.
- When calling the $\text{cursorLeft}$ method, we pop characters from the $\text{left}$ stack up to $k$ times, then push the popped characters onto the $\text{right}$ stack one by one, and finally return up to 10 characters from the $\text{left}$ stack. The time complexity is $O(k)$.
- When calling the $\text{cursorRight}$ method, we pop characters from the $\text{right}$ stack up to $k$ times, then push the popped characters onto the $\text{left}$ stack one by one, and finally return up to 10 characters from the $\text{left}$ stack. The time complexity is $O(k)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -349,6 +349,59 @@ class TextEditor {
*/
```

#### Rust

```rust
struct TextEditor {
left: String,
right: String,
}

impl TextEditor {
fn new() -> Self {
TextEditor {
left: String::new(),
right: String::new(),
}
}

fn add_text(&mut self, text: String) {
self.left.push_str(&text);
}

fn delete_text(&mut self, k: i32) -> i32 {
let k = k.min(self.left.len() as i32) as usize;
self.left.truncate(self.left.len() - k);
k as i32
}

fn cursor_left(&mut self, k: i32) -> String {
let k = k.min(self.left.len() as i32) as usize;
for _ in 0..k {
if let Some(c) = self.left.pop() {
self.right.push(c);
}
}
self.get_last_10_chars()
}

fn cursor_right(&mut self, k: i32) -> String {
let k = k.min(self.right.len() as i32) as usize;
for _ in 0..k {
if let Some(c) = self.right.pop() {
self.left.push(c);
}
}
self.get_last_10_chars()
}

fn get_last_10_chars(&self) -> String {
let len = self.left.len();
self.left[len.saturating_sub(10)..].to_string()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
48 changes: 48 additions & 0 deletions solution/2200-2299/2296.Design a Text Editor/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
struct TextEditor {
left: String,
right: String,
}

impl TextEditor {
fn new() -> Self {
TextEditor {
left: String::new(),
right: String::new(),
}
}

fn add_text(&mut self, text: String) {
self.left.push_str(&text);
}

fn delete_text(&mut self, k: i32) -> i32 {
let k = k.min(self.left.len() as i32) as usize;
self.left.truncate(self.left.len() - k);
k as i32
}

fn cursor_left(&mut self, k: i32) -> String {
let k = k.min(self.left.len() as i32) as usize;
for _ in 0..k {
if let Some(c) = self.left.pop() {
self.right.push(c);
}
}
self.get_last_10_chars()
}

fn cursor_right(&mut self, k: i32) -> String {
let k = k.min(self.right.len() as i32) as usize;
for _ in 0..k {
if let Some(c) = self.right.pop() {
self.left.push(c);
}
}
self.get_last_10_chars()
}

fn get_last_10_chars(&self) -> String {
let len = self.left.len();
self.left[len.saturating_sub(10)..].to_string()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3460.Longest%20Common%20Prefix%20After%20at%20Most%20One%20Removal/README.md
tags:
- 双指针
- 字符串
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3460.Longest%20Common%20Prefix%20After%20at%20Most%20One%20Removal/README_EN.md
tags:
- Two Pointers
- String
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3461.Check%20If%20Digits%20Are%20Equal%20in%20String%20After%20Operations%20I/README.md
tags:
- 数学
- 字符串
- 组合数学
- 数论
- 模拟
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3461.Check%20If%20Digits%20Are%20Equal%20in%20String%20After%20Operations%20I/README_EN.md
tags:
- Math
- String
- Combinatorics
- Number Theory
- Simulation
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3462.Maximum%20Sum%20With%20at%20Most%20K%20Elements/README.md
tags:
- 贪心
- 数组
- 矩阵
- 排序
- 堆(优先队列)
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3462.Maximum%20Sum%20With%20at%20Most%20K%20Elements/README_EN.md
tags:
- Greedy
- Array
- Matrix
- Sorting
- Heap (Priority Queue)
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3463.Check%20If%20Digits%20Are%20Equal%20in%20String%20After%20Operations%20II/README.md
tags:
- 数学
- 字符串
- 组合数学
- 数论
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3463.Check%20If%20Digits%20Are%20Equal%20in%20String%20After%20Operations%20II/README_EN.md
tags:
- Math
- String
- Combinatorics
- Number Theory
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3464.Maximize%20the%20Distance%20Between%20Points%20on%20a%20Square/README.md
tags:
- 贪心
- 数组
- 二分查找
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3464.Maximize%20the%20Distance%20Between%20Points%20on%20a%20Square/README_EN.md
tags:
- Greedy
- Array
- Binary Search
---

<!-- problem:start -->
Expand Down
Loading