Skip to content

Commit 584991e

Browse files
authored
feat: update lc problems (doocs#4113)
1 parent be22fed commit 584991e

File tree

19 files changed

+269
-66
lines changed

19 files changed

+269
-66
lines changed

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

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ textEditor.deleteText(4); // 返回 4
6262
// 删除了 4 个字符。
6363
textEditor.addText("practice"); // 当前文本为 "leetpractice|" 。
6464
textEditor.cursorRight(3); // 返回 "etpractice"
65-
// 当前文本为 "leetpractice|".
65+
// 当前文本为 "leetpractice|".
6666
// 光标无法移动到文本以外,所以无法移动。
6767
// "etpractice" 是光标左边的 10 个字符。
6868
textEditor.cursorLeft(8); // 返回 "leet"
@@ -102,12 +102,12 @@ textEditor.cursorRight(6); // 返回 "practi"
102102

103103
### 方法一:左右栈
104104

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

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

112112
<!-- tabs:start -->
113113

@@ -352,6 +352,59 @@ class TextEditor {
352352
*/
353353
```
354354

355+
#### Rust
356+
357+
```rust
358+
struct TextEditor {
359+
left: String,
360+
right: String,
361+
}
362+
363+
impl TextEditor {
364+
fn new() -> Self {
365+
TextEditor {
366+
left: String::new(),
367+
right: String::new(),
368+
}
369+
}
370+
371+
fn add_text(&mut self, text: String) {
372+
self.left.push_str(&text);
373+
}
374+
375+
fn delete_text(&mut self, k: i32) -> i32 {
376+
let k = k.min(self.left.len() as i32) as usize;
377+
self.left.truncate(self.left.len() - k);
378+
k as i32
379+
}
380+
381+
fn cursor_left(&mut self, k: i32) -> String {
382+
let k = k.min(self.left.len() as i32) as usize;
383+
for _ in 0..k {
384+
if let Some(c) = self.left.pop() {
385+
self.right.push(c);
386+
}
387+
}
388+
self.get_last_10_chars()
389+
}
390+
391+
fn cursor_right(&mut self, k: i32) -> String {
392+
let k = k.min(self.right.len() as i32) as usize;
393+
for _ in 0..k {
394+
if let Some(c) = self.right.pop() {
395+
self.left.push(c);
396+
}
397+
}
398+
self.get_last_10_chars()
399+
}
400+
401+
fn get_last_10_chars(&self) -> String {
402+
let len = self.left.len();
403+
self.left[len.saturating_sub(10)..].to_string()
404+
}
405+
}
406+
```
407+
355408
<!-- tabs:end -->
356409

357410
<!-- solution:end -->

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

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ tags:
5757
TextEditor textEditor = new TextEditor(); // The current text is &quot;|&quot;. (The &#39;|&#39; character represents the cursor)
5858
textEditor.addText(&quot;leetcode&quot;); // The current text is &quot;leetcode|&quot;.
5959
textEditor.deleteText(4); // return 4
60-
// The current text is &quot;leet|&quot;.
60+
// The current text is &quot;leet|&quot;.
6161
// 4 characters were deleted.
62-
textEditor.addText(&quot;practice&quot;); // The current text is &quot;leetpractice|&quot;.
62+
textEditor.addText(&quot;practice&quot;); // The current text is &quot;leetpractice|&quot;.
6363
textEditor.cursorRight(3); // return &quot;etpractice&quot;
64-
// The current text is &quot;leetpractice|&quot;.
64+
// The current text is &quot;leetpractice|&quot;.
6565
// The cursor cannot be moved beyond the actual text and thus did not move.
6666
// &quot;etpractice&quot; is the last 10 characters to the left of the cursor.
6767
textEditor.cursorLeft(8); // return &quot;leet&quot;
@@ -72,7 +72,7 @@ textEditor.deleteText(10); // return 4
7272
// Only 4 characters were deleted.
7373
textEditor.cursorLeft(2); // return &quot;&quot;
7474
// The current text is &quot;|practice&quot;.
75-
// The cursor cannot be moved beyond the actual text and thus did not move.
75+
// The cursor cannot be moved beyond the actual text and thus did not move.
7676
// &quot;&quot; is the last min(10, 0) = 0 characters to the left of the cursor.
7777
textEditor.cursorRight(6); // return &quot;practi&quot;
7878
// The current text is &quot;practi|ce&quot;.
@@ -99,12 +99,12 @@ textEditor.cursorRight(6); // return &quot;practi&quot;
9999

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

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.
102+
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.
103103

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(|\textit{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)$.
104+
- 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}|)$.
105+
- When calling the $\text{deleteText}$ method, we pop characters from the $\text{left}$ stack up to $k$ times. The time complexity is $O(k)$.
106+
- 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)$.
107+
- 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)$.
108108

109109
<!-- tabs:start -->
110110

@@ -349,6 +349,59 @@ class TextEditor {
349349
*/
350350
```
351351

352+
#### Rust
353+
354+
```rust
355+
struct TextEditor {
356+
left: String,
357+
right: String,
358+
}
359+
360+
impl TextEditor {
361+
fn new() -> Self {
362+
TextEditor {
363+
left: String::new(),
364+
right: String::new(),
365+
}
366+
}
367+
368+
fn add_text(&mut self, text: String) {
369+
self.left.push_str(&text);
370+
}
371+
372+
fn delete_text(&mut self, k: i32) -> i32 {
373+
let k = k.min(self.left.len() as i32) as usize;
374+
self.left.truncate(self.left.len() - k);
375+
k as i32
376+
}
377+
378+
fn cursor_left(&mut self, k: i32) -> String {
379+
let k = k.min(self.left.len() as i32) as usize;
380+
for _ in 0..k {
381+
if let Some(c) = self.left.pop() {
382+
self.right.push(c);
383+
}
384+
}
385+
self.get_last_10_chars()
386+
}
387+
388+
fn cursor_right(&mut self, k: i32) -> String {
389+
let k = k.min(self.right.len() as i32) as usize;
390+
for _ in 0..k {
391+
if let Some(c) = self.right.pop() {
392+
self.left.push(c);
393+
}
394+
}
395+
self.get_last_10_chars()
396+
}
397+
398+
fn get_last_10_chars(&self) -> String {
399+
let len = self.left.len();
400+
self.left[len.saturating_sub(10)..].to_string()
401+
}
402+
}
403+
```
404+
352405
<!-- tabs:end -->
353406

354407
<!-- solution:end -->
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
struct TextEditor {
2+
left: String,
3+
right: String,
4+
}
5+
6+
impl TextEditor {
7+
fn new() -> Self {
8+
TextEditor {
9+
left: String::new(),
10+
right: String::new(),
11+
}
12+
}
13+
14+
fn add_text(&mut self, text: String) {
15+
self.left.push_str(&text);
16+
}
17+
18+
fn delete_text(&mut self, k: i32) -> i32 {
19+
let k = k.min(self.left.len() as i32) as usize;
20+
self.left.truncate(self.left.len() - k);
21+
k as i32
22+
}
23+
24+
fn cursor_left(&mut self, k: i32) -> String {
25+
let k = k.min(self.left.len() as i32) as usize;
26+
for _ in 0..k {
27+
if let Some(c) = self.left.pop() {
28+
self.right.push(c);
29+
}
30+
}
31+
self.get_last_10_chars()
32+
}
33+
34+
fn cursor_right(&mut self, k: i32) -> String {
35+
let k = k.min(self.right.len() as i32) as usize;
36+
for _ in 0..k {
37+
if let Some(c) = self.right.pop() {
38+
self.left.push(c);
39+
}
40+
}
41+
self.get_last_10_chars()
42+
}
43+
44+
fn get_last_10_chars(&self) -> String {
45+
let len = self.left.len();
46+
self.left[len.saturating_sub(10)..].to_string()
47+
}
48+
}

solution/3400-3499/3460.Longest Common Prefix After at Most One Removal/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3460.Longest%20Common%20Prefix%20After%20at%20Most%20One%20Removal/README.md
5+
tags:
6+
- 双指针
7+
- 字符串
58
---
69

710
<!-- problem:start -->

solution/3400-3499/3460.Longest Common Prefix After at Most One Removal/README_EN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3460.Longest%20Common%20Prefix%20After%20at%20Most%20One%20Removal/README_EN.md
5+
tags:
6+
- Two Pointers
7+
- String
58
---
69

710
<!-- problem:start -->

solution/3400-3499/3461.Check If Digits Are Equal in String After Operations I/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
comments: true
33
difficulty: 简单
44
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
5+
tags:
6+
- 数学
7+
- 字符串
8+
- 组合数学
9+
- 数论
10+
- 模拟
511
---
612

713
<!-- problem:start -->

solution/3400-3499/3461.Check If Digits Are Equal in String After Operations I/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
comments: true
33
difficulty: Easy
44
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
5+
tags:
6+
- Math
7+
- String
8+
- Combinatorics
9+
- Number Theory
10+
- Simulation
511
---
612

713
<!-- problem:start -->

solution/3400-3499/3462.Maximum Sum With at Most K Elements/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3462.Maximum%20Sum%20With%20at%20Most%20K%20Elements/README.md
5+
tags:
6+
- 贪心
7+
- 数组
8+
- 矩阵
9+
- 排序
10+
- 堆(优先队列)
511
---
612

713
<!-- problem:start -->

solution/3400-3499/3462.Maximum Sum With at Most K Elements/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3462.Maximum%20Sum%20With%20at%20Most%20K%20Elements/README_EN.md
5+
tags:
6+
- Greedy
7+
- Array
8+
- Matrix
9+
- Sorting
10+
- Heap (Priority Queue)
511
---
612

713
<!-- problem:start -->

solution/3400-3499/3463.Check If Digits Are Equal in String After Operations II/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
comments: true
33
difficulty: 困难
44
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
5+
tags:
6+
- 数学
7+
- 字符串
8+
- 组合数学
9+
- 数论
510
---
611

712
<!-- problem:start -->

0 commit comments

Comments
 (0)