From c3be0dd2497b2c60d6a104f584d954a385311802 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 3 Oct 2024 19:57:22 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.1929,1933 * No.1929.Concatenation of Array * No.1933.Check if String Is Decomposable Into Value-Equal Substrings --- .../1929.Concatenation of Array/README.md | 10 +++-- .../1929.Concatenation of Array/README_EN.md | 10 +++-- .../1929.Concatenation of Array/Solution.js | 4 +- .../README.md | 38 ++----------------- .../README_EN.md | 38 ++----------------- .../Solution2.py | 11 ------ 6 files changed, 21 insertions(+), 90 deletions(-) delete mode 100644 solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution2.py diff --git a/solution/1900-1999/1929.Concatenation of Array/README.md b/solution/1900-1999/1929.Concatenation of Array/README.md index cb5bfa919efac..c378e6909f0ca 100644 --- a/solution/1900-1999/1929.Concatenation of Array/README.md +++ b/solution/1900-1999/1929.Concatenation of Array/README.md @@ -67,7 +67,11 @@ tags: -### 方法一 +### 方法一:模拟 + +我们直接根据题目描述模拟,将 $\textit{nums}$ 中的元素依次添加到答案数组中,然后再将 $\textit{nums}$ 中的元素再次添加到答案数组中。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -142,9 +146,7 @@ impl Solution { * @return {number[]} */ var getConcatenation = function (nums) { - let ans = nums.slice(); - ans.splice(nums.length, 0, ...nums); - return ans; + return [...nums, ...nums]; }; ``` diff --git a/solution/1900-1999/1929.Concatenation of Array/README_EN.md b/solution/1900-1999/1929.Concatenation of Array/README_EN.md index bc384edba3963..26c0427d37fca 100644 --- a/solution/1900-1999/1929.Concatenation of Array/README_EN.md +++ b/solution/1900-1999/1929.Concatenation of Array/README_EN.md @@ -60,7 +60,11 @@ tags: -### Solution 1 +### Solution 1: Simulation + +We directly simulate according to the problem description by adding the elements of $\textit{nums}$ to the answer array one by one, and then adding the elements of $\textit{nums}$ to the answer array again. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -135,9 +139,7 @@ impl Solution { * @return {number[]} */ var getConcatenation = function (nums) { - let ans = nums.slice(); - ans.splice(nums.length, 0, ...nums); - return ans; + return [...nums, ...nums]; }; ``` diff --git a/solution/1900-1999/1929.Concatenation of Array/Solution.js b/solution/1900-1999/1929.Concatenation of Array/Solution.js index 86c0ff6a81ef2..a920cb19b9d40 100644 --- a/solution/1900-1999/1929.Concatenation of Array/Solution.js +++ b/solution/1900-1999/1929.Concatenation of Array/Solution.js @@ -3,7 +3,5 @@ * @return {number[]} */ var getConcatenation = function (nums) { - let ans = nums.slice(); - ans.splice(nums.length, 0, ...nums); - return ans; + return [...nums, ...nums]; }; diff --git a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README.md b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README.md index f49d208cd3ddb..89286d45008fb 100644 --- a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README.md +++ b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README.md @@ -82,18 +82,14 @@ tags: ```python class Solution: def isDecomposable(self, s: str) -> bool: - i, n = 0, len(s) cnt2 = 0 - while i < n: - j = i - while j < n and s[j] == s[i]: - j += 1 - if (j - i) % 3 == 1: + for _, g in groupby(s): + m = len(list(g)) + if m % 3 == 1: return False - cnt2 += (j - i) % 3 == 2 + cnt2 += m % 3 == 2 if cnt2 > 1: return False - i = j return cnt2 == 1 ``` @@ -201,30 +197,4 @@ function isDecomposable(s: string): boolean { - - -### 方法二 - - - -#### Python3 - -```python -class Solution: - def isDecomposable(self, s: str) -> bool: - cnt2 = 0 - for _, g in groupby(s): - m = len(list(g)) - if m % 3 == 1: - return False - cnt2 += m % 3 == 2 - if cnt2 > 1: - return False - return cnt2 == 1 -``` - - - - - diff --git a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README_EN.md b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README_EN.md index 10136446eb960..889d99fa3ac1b 100644 --- a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README_EN.md +++ b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README_EN.md @@ -83,18 +83,14 @@ The time complexity is $O(n)$, where $n$ is the length of the string $s$. The sp ```python class Solution: def isDecomposable(self, s: str) -> bool: - i, n = 0, len(s) cnt2 = 0 - while i < n: - j = i - while j < n and s[j] == s[i]: - j += 1 - if (j - i) % 3 == 1: + for _, g in groupby(s): + m = len(list(g)) + if m % 3 == 1: return False - cnt2 += (j - i) % 3 == 2 + cnt2 += m % 3 == 2 if cnt2 > 1: return False - i = j return cnt2 == 1 ``` @@ -202,30 +198,4 @@ function isDecomposable(s: string): boolean { - - -### Solution 2 - - - -#### Python3 - -```python -class Solution: - def isDecomposable(self, s: str) -> bool: - cnt2 = 0 - for _, g in groupby(s): - m = len(list(g)) - if m % 3 == 1: - return False - cnt2 += m % 3 == 2 - if cnt2 > 1: - return False - return cnt2 == 1 -``` - - - - - diff --git a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution2.py b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution2.py deleted file mode 100644 index 5b938278d2632..0000000000000 --- a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution2.py +++ /dev/null @@ -1,11 +0,0 @@ -class Solution: - def isDecomposable(self, s: str) -> bool: - cnt2 = 0 - for _, g in groupby(s): - m = len(list(g)) - if m % 3 == 1: - return False - cnt2 += m % 3 == 2 - if cnt2 > 1: - return False - return cnt2 == 1