Skip to content

Commit 6bede49

Browse files
authored
feat: add solution to lc problem: No.38 && fix: title to No.32 (doocs#2079)
1 parent e26a69a commit 6bede49

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
lines changed

solution/0000-0099/0032.Longest Valid Parentheses/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ var longestValidParentheses = function (s) {
341341
};
342342
```
343343

344-
### Rust
344+
### **Rust**
345345

346346
```rust
347347
impl Solution {

solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ var longestValidParentheses = function (s) {
332332
};
333333
```
334334

335-
### Rust
335+
### **Rust**
336336

337337
```rust
338338
impl Solution {

solution/0000-0099/0038.Count and Say/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
7171

7272
## 解法
7373

74+
**方法一: 模拟**
75+
76+
题目要求输出第 $n$ 项的外观序列,而第 $n$ 项是序列中第 $n-1$ 项的描述。所以我们遍历 $n-1$ 次,每次迭代用快慢指针j和i,分别记录当前字符的位置以及下一个不等于当前字符的位置,更新上一项的序列为 $j-i$ 个当前字符。
77+
78+
时间复杂度:
79+
80+
1. 外部循环迭代 `for _ in range(n - 1)`,这会执行 `n-1` 次。
81+
2. 在内部循环中,我们遍历了字符串`s`, 长度最大为上一项的长度。
82+
3. 内部循环嵌套循环执行了一些基本操作,如比较和字符串拼接,这些基本操作的复杂度可以视为 $O(1)$ 。
83+
84+
综合考虑,整体时间复杂度为 $O(n \times m)$, 其中 n 是要生成的序列的项数, m 是前一项的最大长度。
85+
86+
空间复杂度: $O(m)$, 其中 m 是前一项的最大长度。
87+
7488
<!-- 这里可写通用的实现逻辑 -->
7589

7690
<!-- tabs:start -->
@@ -260,6 +274,32 @@ function countAndSay(n: number): string {
260274
}
261275
```
262276

277+
### **Rust**
278+
279+
```rust
280+
use std::iter::once;
281+
282+
impl Solution {
283+
pub fn count_and_say(n: i32) -> String {
284+
(1..n)
285+
.fold(vec![1], |curr, _| {
286+
let mut next = vec![];
287+
let mut slow = 0;
288+
for fast in 0..=curr.len() {
289+
if fast == curr.len() || curr[slow] != curr[fast] {
290+
next.extend(once((fast - slow) as u8).chain(once(curr[slow])));
291+
slow = fast;
292+
}
293+
}
294+
next
295+
})
296+
.into_iter()
297+
.map(|digit| (digit + b'0') as char)
298+
.collect()
299+
}
300+
}
301+
```
302+
263303
### **...**
264304

265305
```

solution/0000-0099/0038.Count and Say/README_EN.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ countAndSay(4) = say &quot;21&quot; = one 2 + one 1 = &quot;12&quot; + &quot;11&
4747

4848
## Solutions
4949

50+
**Solution 1: Simulation**
51+
52+
The task requires outputting the appearance sequence of the $n$-th item, where the $n$-th item is the description of the $n-1$-th item in the sequence. Therefore, we iterate $n-1$ times. In each iteration, we use fast and slow pointers, denoted as j and i respectively, to record the current character's position and the position of the next character that is not equal to the current character. We then update the sequence of the previous item to be $j-i$ occurrences of the current character.
53+
54+
Time Complexity:
55+
56+
1. The outer loop runs `n - 1` times, iterating to generate the "Count and Say" sequence up to the nth term.
57+
2. The inner while loop iterates through each character in the current string s and counts the consecutive occurrences of the same character.
58+
3. The inner while loop runs in $O(m)$ time, where m is the length of the current string s.
59+
60+
Overall, the time complexity is $O(n \times m)$, where n is the input parameter representing the term to generate, and m is the maximum length of the string in the sequence.
61+
62+
Space Complexity: $O(m)$.
63+
5064
<!-- tabs:start -->
5165

5266
### **Python3**
@@ -230,6 +244,32 @@ function countAndSay(n: number): string {
230244
}
231245
```
232246

247+
### **Rust**
248+
249+
```rust
250+
use std::iter::once;
251+
252+
impl Solution {
253+
pub fn count_and_say(n: i32) -> String {
254+
(1..n)
255+
.fold(vec![1], |curr, _| {
256+
let mut next = vec![];
257+
let mut slow = 0;
258+
for fast in 0..=curr.len() {
259+
if fast == curr.len() || curr[slow] != curr[fast] {
260+
next.extend(once((fast - slow) as u8).chain(once(curr[slow])));
261+
slow = fast;
262+
}
263+
}
264+
next
265+
})
266+
.into_iter()
267+
.map(|digit| (digit + b'0') as char)
268+
.collect()
269+
}
270+
}
271+
```
272+
233273
### **...**
234274

235275
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::iter::once;
2+
3+
impl Solution {
4+
pub fn count_and_say(n: i32) -> String {
5+
(1..n)
6+
.fold(vec![1], |curr, _| {
7+
let mut next = vec![];
8+
let mut slow = 0;
9+
for fast in 0..=curr.len() {
10+
if fast == curr.len() || curr[slow] != curr[fast] {
11+
next.extend(once((fast - slow) as u8).chain(once(curr[slow])));
12+
slow = fast;
13+
}
14+
}
15+
next
16+
})
17+
.into_iter()
18+
.map(|digit| (digit + b'0') as char)
19+
.collect()
20+
}
21+
}
22+

0 commit comments

Comments
 (0)