From 2f870c1d78d35cab3266f9f33424955cd222e7d5 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 11 Jul 2024 15:13:43 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.0877,1859 * No.0877.Stone Game * No.1859.Sorting the Sentence --- solution/0800-0899/0877.Stone Game/README.md | 6 +-- .../0800-0899/0877.Stone Game/README_EN.md | 6 +-- .../0800-0899/0877.Stone Game/Solution.cpp | 6 +-- .../1859.Sorting the Sentence/README.md | 30 +-------------- .../1859.Sorting the Sentence/README_EN.md | 38 ++++--------------- .../1859.Sorting the Sentence/Solution.py | 8 ++-- .../1859.Sorting the Sentence/Solution2.py | 7 ---- 7 files changed, 23 insertions(+), 78 deletions(-) delete mode 100644 solution/1800-1899/1859.Sorting the Sentence/Solution2.py diff --git a/solution/0800-0899/0877.Stone Game/README.md b/solution/0800-0899/0877.Stone Game/README.md index 81246c25f5d5a..946fbc853b9c0 100644 --- a/solution/0800-0899/0877.Stone Game/README.md +++ b/solution/0800-0899/0877.Stone Game/README.md @@ -132,16 +132,16 @@ public: int n = piles.size(); int f[n][n]; memset(f, 0, sizeof(f)); - function dfs = [&](int i, int j) -> int { + auto dfs = [&](auto&& dfs, int i, int j) -> int { if (i > j) { return 0; } if (f[i][j]) { return f[i][j]; } - return f[i][j] = max(piles[i] - dfs(i + 1, j), piles[j] - dfs(i, j - 1)); + return f[i][j] = max(piles[i] - dfs(dfs, i + 1, j), piles[j] - dfs(dfs, i, j - 1)); }; - return dfs(0, n - 1) > 0; + return dfs(dfs, 0, n - 1) > 0; } }; ``` diff --git a/solution/0800-0899/0877.Stone Game/README_EN.md b/solution/0800-0899/0877.Stone Game/README_EN.md index d5bd40600b136..8127658b93743 100644 --- a/solution/0800-0899/0877.Stone Game/README_EN.md +++ b/solution/0800-0899/0877.Stone Game/README_EN.md @@ -117,16 +117,16 @@ public: int n = piles.size(); int f[n][n]; memset(f, 0, sizeof(f)); - function dfs = [&](int i, int j) -> int { + auto dfs = [&](auto&& dfs, int i, int j) -> int { if (i > j) { return 0; } if (f[i][j]) { return f[i][j]; } - return f[i][j] = max(piles[i] - dfs(i + 1, j), piles[j] - dfs(i, j - 1)); + return f[i][j] = max(piles[i] - dfs(dfs, i + 1, j), piles[j] - dfs(dfs, i, j - 1)); }; - return dfs(0, n - 1) > 0; + return dfs(dfs, 0, n - 1) > 0; } }; ``` diff --git a/solution/0800-0899/0877.Stone Game/Solution.cpp b/solution/0800-0899/0877.Stone Game/Solution.cpp index a9ee327b9d700..3435a355f58e0 100644 --- a/solution/0800-0899/0877.Stone Game/Solution.cpp +++ b/solution/0800-0899/0877.Stone Game/Solution.cpp @@ -4,15 +4,15 @@ class Solution { int n = piles.size(); int f[n][n]; memset(f, 0, sizeof(f)); - function dfs = [&](int i, int j) -> int { + auto dfs = [&](auto&& dfs, int i, int j) -> int { if (i > j) { return 0; } if (f[i][j]) { return f[i][j]; } - return f[i][j] = max(piles[i] - dfs(i + 1, j), piles[j] - dfs(i, j - 1)); + return f[i][j] = max(piles[i] - dfs(dfs, i + 1, j), piles[j] - dfs(dfs, i, j - 1)); }; - return dfs(0, n - 1) > 0; + return dfs(dfs, 0, n - 1) > 0; } }; \ No newline at end of file diff --git a/solution/1800-1899/1859.Sorting the Sentence/README.md b/solution/1800-1899/1859.Sorting the Sentence/README.md index d48140da04a01..f75ce3a99e79e 100644 --- a/solution/1800-1899/1859.Sorting the Sentence/README.md +++ b/solution/1800-1899/1859.Sorting the Sentence/README.md @@ -66,13 +66,9 @@ tags: ### 方法一:字符串分割 -我们先将字符串 $s$ 按照空格分割,得到字符串数组 $words$。然后,我们创建一个长度为 $|words|$ 的字符串数组 $ans$,用于存放答案。 +我们先将字符串 $s$ 按照空格分割,得到字符串数组 $\textit{ws}$,然后遍历数组 $\textit{ws}$,将每个单词的最后一个字符减去字符 '1',得到的结果作为单词的索引,将单词的前缀作为单词的内容,最后将单词按照索引顺序拼接起来即可。 -接下来,遍历字符串数组 $words$ 中的每个字符串 $w$,找到 $w$ 的最后一个字符表示的位置 $i$,然后将 $w$ 的前 $|w|-1$ 个字符作为新的字符串 $w'$,将 $w'$ 放在数组 $ans$ 的第 $i$ 个位置。 - -最后,将数组 $ans$ 按照空格连接成字符串,即为答案。 - -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。 @@ -176,26 +172,4 @@ var sortSentence = function (s) { - - -### 方法二 - - - -#### Python3 - -```python -class Solution: - def sortSentence(self, s: str) -> str: - ws = s.split() - ans = [None] * len(ws) - for w in ws: - ans[int(w[-1]) - 1] = w[:-1] - return ' '.join(ans) -``` - - - - - diff --git a/solution/1800-1899/1859.Sorting the Sentence/README_EN.md b/solution/1800-1899/1859.Sorting the Sentence/README_EN.md index b859d1d2ad743..8578b480eb04a 100644 --- a/solution/1800-1899/1859.Sorting the Sentence/README_EN.md +++ b/solution/1800-1899/1859.Sorting the Sentence/README_EN.md @@ -83,13 +83,9 @@ tags: ### Solution 1: String Splitting -First, we split the string $s$ by spaces to get the string array $words$. Then, we create a string array $ans$ of length $|words|$ to store the answer. +First, we split the string $s$ by spaces to get the array of strings $\textit{ws}$. Then, we iterate through the array $\textit{ws}$, subtracting the character '1' from the last character of each word to get the result as the index of the word. We take the prefix of the word as the content of the word. Finally, we concatenate the words in index order. -Next, we iterate over each string $w$ in the string array $words$, find the position $i$ represented by the last character of $w$, then take the first $|w|-1$ characters of $w$ as the new string $w'$, and place $w'$ in the $i$th position of the array $ans$. - -Finally, we join the array $ans$ into a string by spaces, which is the answer. - -The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$. +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$. @@ -98,9 +94,11 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is ```python class Solution: def sortSentence(self, s: str) -> str: - ws = [(w[:-1], int(w[-1])) for w in s.split()] - ws.sort(key=lambda x: x[1]) - return ' '.join(w for w, _ in ws) + ws = s.split() + ans = [None] * len(ws) + for w in ws: + ans[int(w[-1]) - 1] = w[:-1] + return " ".join(ans) ``` #### Java @@ -193,26 +191,4 @@ var sortSentence = function (s) { - - -### Solution 2 - - - -#### Python3 - -```python -class Solution: - def sortSentence(self, s: str) -> str: - ws = s.split() - ans = [None] * len(ws) - for w in ws: - ans[int(w[-1]) - 1] = w[:-1] - return ' '.join(ans) -``` - - - - - diff --git a/solution/1800-1899/1859.Sorting the Sentence/Solution.py b/solution/1800-1899/1859.Sorting the Sentence/Solution.py index 23d0851aca4c6..814e1be74184e 100644 --- a/solution/1800-1899/1859.Sorting the Sentence/Solution.py +++ b/solution/1800-1899/1859.Sorting the Sentence/Solution.py @@ -1,5 +1,7 @@ class Solution: def sortSentence(self, s: str) -> str: - ws = [(w[:-1], int(w[-1])) for w in s.split()] - ws.sort(key=lambda x: x[1]) - return ' '.join(w for w, _ in ws) + ws = s.split() + ans = [None] * len(ws) + for w in ws: + ans[int(w[-1]) - 1] = w[:-1] + return " ".join(ans) diff --git a/solution/1800-1899/1859.Sorting the Sentence/Solution2.py b/solution/1800-1899/1859.Sorting the Sentence/Solution2.py deleted file mode 100644 index 749c262928cb2..0000000000000 --- a/solution/1800-1899/1859.Sorting the Sentence/Solution2.py +++ /dev/null @@ -1,7 +0,0 @@ -class Solution: - def sortSentence(self, s: str) -> str: - ws = s.split() - ans = [None] * len(ws) - for w in ws: - ans[int(w[-1]) - 1] = w[:-1] - return ' '.join(ans)