From 7fb016bd54cfb57f8471fd181b7fe430c60c80ae Mon Sep 17 00:00:00 2001 From: herschel-ma Date: Sun, 10 Dec 2023 19:53:33 +0800 Subject: [PATCH 1/3] feat: add solution to lc problem: No.38 --- .../0032.Longest Valid Parentheses/README.md | 2 +- .../README_EN.md | 2 +- .../0000-0099/0038.Count and Say/README.md | 40 +++++++++++++++++++ .../0000-0099/0038.Count and Say/README_EN.md | 40 +++++++++++++++++++ .../0000-0099/0038.Count and Say/Solution.rs | 22 ++++++++++ 5 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 solution/0000-0099/0038.Count and Say/Solution.rs diff --git a/solution/0000-0099/0032.Longest Valid Parentheses/README.md b/solution/0000-0099/0032.Longest Valid Parentheses/README.md index ce178465d30da..ccb3aa2c2a2e4 100644 --- a/solution/0000-0099/0032.Longest Valid Parentheses/README.md +++ b/solution/0000-0099/0032.Longest Valid Parentheses/README.md @@ -341,7 +341,7 @@ var longestValidParentheses = function (s) { }; ``` -### Rust +### **Rust** ```rust impl Solution { diff --git a/solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md b/solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md index e4d4b974df5e9..cea5ab48538eb 100644 --- a/solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md +++ b/solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md @@ -332,7 +332,7 @@ var longestValidParentheses = function (s) { }; ``` -### Rust +### **Rust** ```rust impl Solution { diff --git a/solution/0000-0099/0038.Count and Say/README.md b/solution/0000-0099/0038.Count and Say/README.md index a4e96b3a88f8f..b39a35291532c 100644 --- a/solution/0000-0099/0038.Count and Say/README.md +++ b/solution/0000-0099/0038.Count and Say/README.md @@ -71,6 +71,20 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211" ## 解法 +**方法一: 模拟** + +题目要求输出第 $n$ 项的外观序列,而第 $n$ 项是序列中第 $n-1$ 项的描述。所以我们遍历 $n-1$ 次,每次迭代用快慢指针j和i,分别记录当前字符的位置以及下一个不等于当前字符的位置,更新上一项的序列为 $j-i$ 个当前字符。 + +时间复杂度: + +1. 外部循环迭代 `for _ in range(n - 1)`,这会执行 `n-1` 次。 +2. 在内部循环中,我们遍历了字符串`s`, 长度最大为上一项的长度。 +3. 内部循环嵌套循环执行了一些基本操作,如比较和字符串拼接,这些基本操作的复杂度可以视为 $O(1)$ 。 + +综合考虑,整体时间复杂度为 $O(n * m)$, 其中 n 是要生成的序列的项数, m 是前一项的最大长度。 + +空间复杂度: $O(m)$, 其中 m 是前一项的最大长度。 + @@ -260,6 +274,32 @@ function countAndSay(n: number): string { } ``` +### **Rust** + +```rust +use std::iter::once; + +impl Solution { + pub fn count_and_say(n: i32) -> String { + (1..n) + .fold(vec![1], |curr, _| { + let mut next = vec![]; + let mut slow = 0; + for fast in 0..=curr.len() { + if fast == curr.len() || curr[slow] != curr[fast] { + next.extend(once((fast - slow) as u8).chain(once(curr[slow]))); + slow = fast; + } + } + next + }) + .into_iter() + .map(|digit| (digit + b'0') as char) + .collect() + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0038.Count and Say/README_EN.md b/solution/0000-0099/0038.Count and Say/README_EN.md index 62eaea3da8ed0..839214b71c04c 100644 --- a/solution/0000-0099/0038.Count and Say/README_EN.md +++ b/solution/0000-0099/0038.Count and Say/README_EN.md @@ -47,6 +47,20 @@ countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11& ## Solutions +**Solution 1: Simulation** + +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. + +Time Complexity: + +1. The outer loop runs `n - 1` times, iterating to generate the "Count and Say" sequence up to the nth term. +2. The inner while loop iterates through each character in the current string s and counts the consecutive occurrences of the same character. +3. The inner while loop runs in $O(m)$ time, where m is the length of the current string s. + +Overall, the time complexity is $O(n * m)$, where n is the input parameter representing the term to generate, and m is the maximum length of the string in the sequence. + +Space Complexity: $O(m)$. + ### **Python3** @@ -230,6 +244,32 @@ function countAndSay(n: number): string { } ``` +### **Rust** + +```rust +use std::iter::once; + +impl Solution { + pub fn count_and_say(n: i32) -> String { + (1..n) + .fold(vec![1], |curr, _| { + let mut next = vec![]; + let mut slow = 0; + for fast in 0..=curr.len() { + if fast == curr.len() || curr[slow] != curr[fast] { + next.extend(once((fast - slow) as u8).chain(once(curr[slow]))); + slow = fast; + } + } + next + }) + .into_iter() + .map(|digit| (digit + b'0') as char) + .collect() + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0038.Count and Say/Solution.rs b/solution/0000-0099/0038.Count and Say/Solution.rs new file mode 100644 index 0000000000000..e6a11b4d3ffaf --- /dev/null +++ b/solution/0000-0099/0038.Count and Say/Solution.rs @@ -0,0 +1,22 @@ +use std::iter::once; + +impl Solution { + pub fn count_and_say(n: i32) -> String { + (1..n) + .fold(vec![1], |curr, _| { + let mut next = vec![]; + let mut slow = 0; + for fast in 0..=curr.len() { + if fast == curr.len() || curr[slow] != curr[fast] { + next.extend(once((fast - slow) as u8).chain(once(curr[slow]))); + slow = fast; + } + } + next + }) + .into_iter() + .map(|digit| (digit + b'0') as char) + .collect() + } +} + From 0da8eb7e5c42e8e33968ce3d158a20c0c26ee1b1 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 10 Dec 2023 20:38:40 +0800 Subject: [PATCH 2/3] Update README.md --- solution/0000-0099/0038.Count and Say/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0000-0099/0038.Count and Say/README.md b/solution/0000-0099/0038.Count and Say/README.md index b39a35291532c..99c5aa8ebde2d 100644 --- a/solution/0000-0099/0038.Count and Say/README.md +++ b/solution/0000-0099/0038.Count and Say/README.md @@ -81,7 +81,7 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211" 2. 在内部循环中,我们遍历了字符串`s`, 长度最大为上一项的长度。 3. 内部循环嵌套循环执行了一些基本操作,如比较和字符串拼接,这些基本操作的复杂度可以视为 $O(1)$ 。 -综合考虑,整体时间复杂度为 $O(n * m)$, 其中 n 是要生成的序列的项数, m 是前一项的最大长度。 +综合考虑,整体时间复杂度为 $O(n \times m)$, 其中 n 是要生成的序列的项数, m 是前一项的最大长度。 空间复杂度: $O(m)$, 其中 m 是前一项的最大长度。 From a67dc45d5e409fc852ebc4e4993db0a5277bddff Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 10 Dec 2023 20:39:34 +0800 Subject: [PATCH 3/3] Update README_EN.md --- solution/0000-0099/0038.Count and Say/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0000-0099/0038.Count and Say/README_EN.md b/solution/0000-0099/0038.Count and Say/README_EN.md index 839214b71c04c..5157477bb886a 100644 --- a/solution/0000-0099/0038.Count and Say/README_EN.md +++ b/solution/0000-0099/0038.Count and Say/README_EN.md @@ -57,7 +57,7 @@ Time Complexity: 2. The inner while loop iterates through each character in the current string s and counts the consecutive occurrences of the same character. 3. The inner while loop runs in $O(m)$ time, where m is the length of the current string s. -Overall, the time complexity is $O(n * m)$, where n is the input parameter representing the term to generate, and m is the maximum length of the string in the sequence. +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. Space Complexity: $O(m)$.