Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 42054c2

Browse files
authored
feat: update lc problems (doocs#4271)
1 parent 72ab1fd commit 42054c2

File tree

75 files changed

+14623
-15360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+14623
-15360
lines changed

‎solution/0100-0199/0195.Tenth Line/README_EN.md‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,26 @@ tags:
2323
<p>Assume that <code>file.txt</code> has the following content:</p>
2424

2525
<pre>
26-
2726
Line 1
28-
2927
Line 2
30-
3128
Line 3
32-
3329
Line 4
34-
3530
Line 5
36-
3731
Line 6
38-
3932
Line 7
40-
4133
Line 8
42-
4334
Line 9
44-
4535
Line 10
46-
4736
</pre>
4837

4938
<p>Your script should output the tenth line, which is:</p>
5039

5140
<pre>
52-
5341
Line 10
54-
5542
</pre>
5643

5744
<div class="spoilers"><b>Note:</b><br />
58-
5945
1. If the file contains less than 10 lines, what should you output?<br />
60-
6146
2. There&#39;s at least three different solutions. Try to explore all possibilities.</div>
6247

6348
<!-- description:end -->

‎solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ tags:
3232
<p><strong>示例 1:</strong></p>
3333

3434
<pre><strong>输入:</strong> root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
35-
<strong>输出:</strong> 6
35+
<strong>输出:</strong> 6
3636
<strong>解释: </strong>节点 <code>2 </code>和节点 <code>8 </code>的最近公共祖先是 <code>6。</code>
3737
</pre>
3838

‎solution/0400-0499/0486.Predict the Winner/README.md‎

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,26 @@ tags:
6363

6464
### 方法一:记忆化搜索
6565

66-
我们设计一个函数 $dfs(i, j),ドル表示从第 $i$ 个数到第 $j$ 个数,当前玩家与另一个玩家的得分之差的最大值。那么答案就是 $dfs(0, n - 1) \gt 0$。
66+
我们设计一个函数 $\textit{dfs}(i, j),ドル表示从第 $i$ 个数到第 $j$ 个数,当前玩家与另一个玩家的得分之差的最大值。那么答案就是 $\textit{dfs}(0, n - 1) \geq 0$。
6767

68-
函数 $dfs(i, j)$ 的计算方法如下:
68+
函数 $\textit{dfs}(i, j)$ 的计算方法如下:
6969

70-
- 如果 $i \gt j,ドル说明当前没有数字了,所以当前玩家没有分数可以拿,差值为 0ドル,ドル即 $dfs(i, j) = 0$。
71-
- 否则,当前玩家有两种选择,如果选择第 $i$ 个数,那么当前玩家与另一个玩家的得分之差为 $nums[i] - dfs(i + 1, j)$;如果选择第 $j$ 个数,那么当前玩家与另一个玩家的得分之差为 $nums[j] - dfs(i, j - 1)$。当前玩家会选择两种情况中差值较大的情况,也就是说 $dfs(i, j) = \max(nums[i] - dfs(i + 1, j), nums[j] - dfs(i, j - 1))$。
70+
- 如果 $i > j,ドル说明当前没有数字了,所以当前玩家没有分数可以拿,差值为 0ドル,ドル即 $\textit{dfs}(i, j) = 0$。
71+
- 否则,当前玩家有两种选择,如果选择第 $i$ 个数,那么当前玩家与另一个玩家的得分之差为 $\textit{nums}[i] - \textit{dfs}(i + 1, j)$;如果选择第 $j$ 个数,那么当前玩家与另一个玩家的得分之差为 $\textit{nums}[j] - \textit{dfs}(i, j - 1)$。当前玩家会选择两种情况中差值较大的情况,也就是说 $\textit{dfs}(i, j) = \max(\textit{nums}[i] - \textit{dfs}(i + 1, j), \textit{nums}[j] - \textit{dfs}(i, j - 1))$。
7272

73-
最后,我们只需要判断 $dfs(0, n - 1) \gt 0$ 即可。
73+
最后,我们只需要判断 $\textit{dfs}(0, n - 1) \geq 0$ 即可。
7474

75-
为了避免重复计算,我们可以使用记忆化搜索的方法,用一个数组 $f$ 记录所有的 $dfs(i, j)$ 的值,当函数再次被调用到时,我们可以直接从 $f$ 中取出答案而不需要重新计算。
75+
为了避免重复计算,我们可以使用记忆化搜索的方法,用一个数组 $f$ 记录所有的 $\textit{dfs}(i, j)$ 的值,当函数再次被调用到时,我们可以直接从 $f$ 中取出答案而不需要重新计算。
7676

77-
时间复杂度 $O(n^2),ドル空间复杂度 $O(n^2)$。其中 $n$ 是数组的长度
77+
时间复杂度 $O(n^2),ドル空间复杂度 $O(n^2)$。其中 $n$ 是数组 $\textit{nums}$ 的长度
7878

7979
<!-- tabs:start -->
8080

8181
#### Python3
8282

8383
```python
8484
class Solution:
85-
def PredictTheWinner(self, nums: List[int]) -> bool:
85+
def predictTheWinner(self, nums: List[int]) -> bool:
8686
@cache
8787
def dfs(i: int, j: int) -> int:
8888
if i > j:
@@ -99,7 +99,7 @@ class Solution {
9999
private int[] nums;
100100
private int[][] f;
101101

102-
public boolean PredictTheWinner(int[] nums) {
102+
public boolean predictTheWinner(int[] nums) {
103103
this.nums = nums;
104104
int n = nums.length;
105105
f = new int[n][n];
@@ -123,11 +123,10 @@ class Solution {
123123
```cpp
124124
class Solution {
125125
public:
126-
bool PredictTheWinner(vector<int>& nums) {
126+
bool predictTheWinner(vector<int>& nums) {
127127
int n = nums.size();
128-
int f[n][n];
129-
memset(f, 0, sizeof(f));
130-
function<int(int, int)> dfs = [&](int i, int j) -> int {
128+
vector<vector<int>> f(n, vector<int>(n));
129+
auto dfs = [&](this auto&& dfs, int i, int j) -> int {
131130
if (i > j) {
132131
return 0;
133132
}
@@ -144,7 +143,7 @@ public:
144143
#### Go
145144
146145
```go
147-
func PredictTheWinner(nums []int) bool {
146+
func predictTheWinner(nums []int) bool {
148147
n := len(nums)
149148
f := make([][]int, n)
150149
for i := range f {
@@ -167,9 +166,9 @@ func PredictTheWinner(nums []int) bool {
167166
#### TypeScript
168167

169168
```ts
170-
function PredictTheWinner(nums: number[]): boolean {
169+
function predictTheWinner(nums: number[]): boolean {
171170
const n = nums.length;
172-
const f: number[][] = newArray(n).fill(0).map(() =>new Array(n).fill(0));
171+
const f: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
173172
const dfs = (i: number, j: number): number => {
174173
if (i > j) {
175174
return 0;
@@ -187,29 +186,24 @@ function PredictTheWinner(nums: number[]): boolean {
187186

188187
```rust
189188
impl Solution {
190-
#[allow(dead_code)]
191189
pub fn predict_the_winner(nums: Vec<i32>) -> bool {
192190
let n = nums.len();
193-
let mut dp: Vec<Vec<i32>> = vec![vec![0; n]; n];
191+
let mut f = vec![vec![0; n]; n];
192+
Self::dfs(&nums, &mut f, 0, n - 1) >= 0
193+
}
194194

195-
// Initialize the dp vector
196-
for i in0..n {
197-
dp[i][i] =nums[i];
195+
fndfs(nums:&Vec<i32>, f:&mutVec<Vec<i32>>, i:usize, j:usize) ->i32 {
196+
if i ==j {
197+
returnnums[i]asi32;
198198
}
199-
200-
// Begin the dp process
201-
for i in (0..n - 1).rev() {
202-
for j in i + 1..n {
203-
dp[i][j] = std::cmp::max(
204-
// Take i-th num
205-
nums[i] - dp[i + 1][j],
206-
// Take j-th num
207-
nums[j] - dp[i][j - 1],
208-
);
209-
}
199+
if f[i][j] != 0 {
200+
return f[i][j];
210201
}
211-
212-
dp[0][n - 1] >= 0
202+
f[i][j] = std::cmp::max(
203+
nums[i] - Self::dfs(nums, f, i + 1, j),
204+
nums[j] - Self::dfs(nums, f, i, j - 1)
205+
);
206+
f[i][j]
213207
}
214208
}
215209
```
@@ -222,20 +216,20 @@ impl Solution {
222216

223217
### 方法二:动态规划
224218

225-
我们也可以使用动态规划的方法,定义 $f[i][j]$ 表示当前玩家在 $nums[i..j]$ 这些数字中能够获得的最大得分的差值。那么最后答案就是 $f[0][n - 1] \gt 0$。
219+
我们也可以使用动态规划的方法,定义 $f[i][j]$ 表示当前玩家在 $\textit{nums}[i..j]$ 这些数字中能够获得的最大得分的差值。那么最后答案就是 $f[0][n - 1] \geq 0$。
226220

227-
初始时 $f[i][i]=nums[i],ドル因为只有一个数,所以当前玩家只能拿取这个数,得分差值为 $nums[i]$。
221+
初始时 $f[i][i]=\textit{nums}[i],ドル因为只有一个数,所以当前玩家只能拿取这个数,得分差值为 $\textit{nums}[i]$。
228222

229-
考虑 $f[i][j],ドル其中 $i \lt j,ドル有两种情况:
223+
考虑 $f[i][j],ドル其中 $i < j,ドル有两种情况:
230224

231-
- 如果当前玩家拿走了 $nums[i],ドル那么剩下的数字为 $nums[i + 1..j],ドル此时轮到另一个玩家进行游戏,所以 $f[i][j] = nums[i] - f[i + 1][j]$。
232-
- 如果当前玩家拿走了 $nums[j],ドル那么剩下的数字为 $nums[i..j - 1],ドル此时轮到另一个玩家进行游戏,所以 $f[i][j] = nums[j] - f[i][j - 1]$。
225+
- 如果当前玩家拿走了 $\textit{nums}[i],ドル那么剩下的数字为 $\textit{nums}[i + 1..j],ドル此时轮到另一个玩家进行游戏,所以 $f[i][j] = \textit{nums}[i] - f[i + 1][j]$。
226+
- 如果当前玩家拿走了 $\textit{nums}[j],ドル那么剩下的数字为 $\textit{nums}[i..j - 1],ドル此时轮到另一个玩家进行游戏,所以 $f[i][j] = \textit{nums}[j] - f[i][j - 1]$。
233227

234-
因此,最终的状态转移方程为 $f[i][j] = \max(nums[i] - f[i + 1][j], nums[j] - f[i][j - 1])$。
228+
因此,最终的状态转移方程为 $f[i][j] = \max(\textit{nums}[i] - f[i + 1][j], \textit{nums}[j] - f[i][j - 1])$。
235229

236-
最后,我们只需要判断 $f[0][n - 1] \gt 0$ 即可。
230+
最后,我们只需要判断 $f[0][n - 1] \geq 0$ 即可。
237231

238-
时间复杂度 $O(n^2),ドル空间复杂度 $O(n^2)$。其中 $n$ 是数组的长度
232+
时间复杂度 $O(n^2),ドル空间复杂度 $O(n^2)$。其中 $n$ 是数组 $\textit{nums}$ 的长度
239233

240234
相似题目:
241235

@@ -247,7 +241,7 @@ impl Solution {
247241

248242
```python
249243
class Solution:
250-
def PredictTheWinner(self, nums: List[int]) -> bool:
244+
def predictTheWinner(self, nums: List[int]) -> bool:
251245
n = len(nums)
252246
f = [[0] * n for _ in range(n)]
253247
for i, x in enumerate(nums):
@@ -262,7 +256,7 @@ class Solution:
262256

263257
```java
264258
class Solution {
265-
public boolean PredictTheWinner(int[] nums) {
259+
public boolean predictTheWinner(int[] nums) {
266260
int n = nums.length;
267261
int[][] f = new int[n][n];
268262
for (int i = 0; i < n; ++i) {
@@ -283,7 +277,7 @@ class Solution {
283277
```cpp
284278
class Solution {
285279
public:
286-
bool PredictTheWinner(vector<int>& nums) {
280+
bool predictTheWinner(vector<int>& nums) {
287281
int n = nums.size();
288282
int f[n][n];
289283
memset(f, 0, sizeof(f));
@@ -303,7 +297,7 @@ public:
303297
#### Go
304298
305299
```go
306-
func PredictTheWinner(nums []int) bool {
300+
func predictTheWinner(nums []int) bool {
307301
n := len(nums)
308302
f := make([][]int, n)
309303
for i, x := range nums {
@@ -322,9 +316,9 @@ func PredictTheWinner(nums []int) bool {
322316
#### TypeScript
323317

324318
```ts
325-
function PredictTheWinner(nums: number[]): boolean {
319+
function predictTheWinner(nums: number[]): boolean {
326320
const n = nums.length;
327-
const f: number[][] = newArray(n).fill(0).map(() =>new Array(n).fill(0));
321+
const f: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
328322
for (let i = 0; i < n; ++i) {
329323
f[i][i] = nums[i];
330324
}
@@ -337,6 +331,29 @@ function PredictTheWinner(nums: number[]): boolean {
337331
}
338332
```
339333

334+
#### Rust
335+
336+
```rust
337+
impl Solution {
338+
pub fn predict_the_winner(nums: Vec<i32>) -> bool {
339+
let n = nums.len();
340+
let mut f = vec![vec![0; n]; n];
341+
342+
for i in 0..n {
343+
f[i][i] = nums[i];
344+
}
345+
346+
for i in (0..n - 1).rev() {
347+
for j in i + 1..n {
348+
f[i][j] = std::cmp::max(nums[i] - f[i + 1][j], nums[j] - f[i][j - 1]);
349+
}
350+
}
351+
352+
f[0][n - 1] >= 0
353+
}
354+
}
355+
```
356+
340357
<!-- tabs:end -->
341358

342359
<!-- solution:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /