Skip to content

Commit 5d595de

Browse files
authored
feat: update solutions to lc problems: No.0877,1859 (doocs#3256)
* No.0877.Stone Game * No.1859.Sorting the Sentence
1 parent adad915 commit 5d595de

File tree

7 files changed

+23
-78
lines changed

7 files changed

+23
-78
lines changed

solution/0800-0899/0877.Stone Game/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,16 @@ public:
132132
int n = piles.size();
133133
int f[n][n];
134134
memset(f, 0, sizeof(f));
135-
function<int(int, int)> dfs = [&](int i, int j) -> int {
135+
auto dfs = [&](auto&& dfs, int i, int j) -> int {
136136
if (i > j) {
137137
return 0;
138138
}
139139
if (f[i][j]) {
140140
return f[i][j];
141141
}
142-
return f[i][j] = max(piles[i] - dfs(i + 1, j), piles[j] - dfs(i, j - 1));
142+
return f[i][j] = max(piles[i] - dfs(dfs, i + 1, j), piles[j] - dfs(dfs, i, j - 1));
143143
};
144-
return dfs(0, n - 1) > 0;
144+
return dfs(dfs, 0, n - 1) > 0;
145145
}
146146
};
147147
```

solution/0800-0899/0877.Stone Game/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@ public:
117117
int n = piles.size();
118118
int f[n][n];
119119
memset(f, 0, sizeof(f));
120-
function<int(int, int)> dfs = [&](int i, int j) -> int {
120+
auto dfs = [&](auto&& dfs, int i, int j) -> int {
121121
if (i > j) {
122122
return 0;
123123
}
124124
if (f[i][j]) {
125125
return f[i][j];
126126
}
127-
return f[i][j] = max(piles[i] - dfs(i + 1, j), piles[j] - dfs(i, j - 1));
127+
return f[i][j] = max(piles[i] - dfs(dfs, i + 1, j), piles[j] - dfs(dfs, i, j - 1));
128128
};
129-
return dfs(0, n - 1) > 0;
129+
return dfs(dfs, 0, n - 1) > 0;
130130
}
131131
};
132132
```

solution/0800-0899/0877.Stone Game/Solution.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ class Solution {
44
int n = piles.size();
55
int f[n][n];
66
memset(f, 0, sizeof(f));
7-
function<int(int, int)> dfs = [&](int i, int j) -> int {
7+
auto dfs = [&](auto&& dfs, int i, int j) -> int {
88
if (i > j) {
99
return 0;
1010
}
1111
if (f[i][j]) {
1212
return f[i][j];
1313
}
14-
return f[i][j] = max(piles[i] - dfs(i + 1, j), piles[j] - dfs(i, j - 1));
14+
return f[i][j] = max(piles[i] - dfs(dfs, i + 1, j), piles[j] - dfs(dfs, i, j - 1));
1515
};
16-
return dfs(0, n - 1) > 0;
16+
return dfs(dfs, 0, n - 1) > 0;
1717
}
1818
};

solution/1800-1899/1859.Sorting the Sentence/README.md

+2-28
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,9 @@ tags:
6666

6767
### 方法一:字符串分割
6868

69-
我们先将字符串 $s$ 按照空格分割,得到字符串数组 $words$。然后,我们创建一个长度为 $|words|$ 的字符串数组 $ans$,用于存放答案
69+
我们先将字符串 $s$ 按照空格分割,得到字符串数组 $\textit{ws}$,然后遍历数组 $\textit{ws}$,将每个单词的最后一个字符减去字符 '1',得到的结果作为单词的索引,将单词的前缀作为单词的内容,最后将单词按照索引顺序拼接起来即可
7070

71-
接下来,遍历字符串数组 $words$ 中的每个字符串 $w$,找到 $w$ 的最后一个字符表示的位置 $i$,然后将 $w$ 的前 $|w|-1$ 个字符作为新的字符串 $w'$,将 $w'$ 放在数组 $ans$ 的第 $i$ 个位置。
72-
73-
最后,将数组 $ans$ 按照空格连接成字符串,即为答案。
74-
75-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
71+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
7672

7773
<!-- tabs:start -->
7874

@@ -176,26 +172,4 @@ var sortSentence = function (s) {
176172

177173
<!-- solution:end -->
178174

179-
<!-- solution:start -->
180-
181-
### 方法二
182-
183-
<!-- tabs:start -->
184-
185-
#### Python3
186-
187-
```python
188-
class Solution:
189-
def sortSentence(self, s: str) -> str:
190-
ws = s.split()
191-
ans = [None] * len(ws)
192-
for w in ws:
193-
ans[int(w[-1]) - 1] = w[:-1]
194-
return ' '.join(ans)
195-
```
196-
197-
<!-- tabs:end -->
198-
199-
<!-- solution:end -->
200-
201175
<!-- problem:end -->

solution/1800-1899/1859.Sorting the Sentence/README_EN.md

+7-31
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,9 @@ tags:
8383

8484
### Solution 1: String Splitting
8585

86-
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.
86+
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.
8787

88-
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$.
89-
90-
Finally, we join the array $ans$ into a string by spaces, which is the answer.
91-
92-
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
88+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.
9389

9490
<!-- tabs:start -->
9591

@@ -98,9 +94,11 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is
9894
```python
9995
class Solution:
10096
def sortSentence(self, s: str) -> str:
101-
ws = [(w[:-1], int(w[-1])) for w in s.split()]
102-
ws.sort(key=lambda x: x[1])
103-
return ' '.join(w for w, _ in ws)
97+
ws = s.split()
98+
ans = [None] * len(ws)
99+
for w in ws:
100+
ans[int(w[-1]) - 1] = w[:-1]
101+
return " ".join(ans)
104102
```
105103

106104
#### Java
@@ -193,26 +191,4 @@ var sortSentence = function (s) {
193191

194192
<!-- solution:end -->
195193

196-
<!-- solution:start -->
197-
198-
### Solution 2
199-
200-
<!-- tabs:start -->
201-
202-
#### Python3
203-
204-
```python
205-
class Solution:
206-
def sortSentence(self, s: str) -> str:
207-
ws = s.split()
208-
ans = [None] * len(ws)
209-
for w in ws:
210-
ans[int(w[-1]) - 1] = w[:-1]
211-
return ' '.join(ans)
212-
```
213-
214-
<!-- tabs:end -->
215-
216-
<!-- solution:end -->
217-
218194
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
22
def sortSentence(self, s: str) -> str:
3-
ws = [(w[:-1], int(w[-1])) for w in s.split()]
4-
ws.sort(key=lambda x: x[1])
5-
return ' '.join(w for w, _ in ws)
3+
ws = s.split()
4+
ans = [None] * len(ws)
5+
for w in ws:
6+
ans[int(w[-1]) - 1] = w[:-1]
7+
return " ".join(ans)

solution/1800-1899/1859.Sorting the Sentence/Solution2.py

-7
This file was deleted.

0 commit comments

Comments
 (0)