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 c33bcc7

Browse files
feat: add solutions to lc problems: No.0292,0293 (doocs#2308)
1 parent 859c150 commit c33bcc7

File tree

9 files changed

+148
-89
lines changed

9 files changed

+148
-89
lines changed

‎solution/0200-0299/0292.Nim Game/README.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,20 @@
5555

5656
## 解法
5757

58-
### 方法一:数学推理
58+
### 方法一:找规律
5959

6060
第一个得到 4ドル$ 的倍数(即 $n$ 能被 4ドル$ 整除)的将会输掉比赛。
6161

6262
证明:
6363

64-
1. 当 $n=4,ドル无论第一个玩家选择 1ドル/2/3$ 哪个数字,第二个玩家总能选择剩下的数字,**第一个玩家将会输掉比赛**
65-
1. 当 4ドル<n<8,ドル即 ($n=5,6,7$),第一个玩家可以相应地将数字减少为 4ドル,ドル那么 4ドル$ 这个死亡数字给到了第二个玩家,第二个玩家将会输掉比赛。
66-
1. 当 $n=8,ドル无论第一个玩家选择 1ドル/2/3$ 哪个数字,都会把 4ドル<n<8$ 的数字留给第二个,**第一个玩家将会输掉比赛**
64+
1. 当 $n \lt 4$ 时,第一个玩家可以直接拿走所有的石头,所以第一个玩家将会赢得比赛。
65+
1. 当 $n = 4,ドル无论第一个玩家选择 1,ドル 2, 3$ 哪个数字,第二个玩家总能选择剩下的数字,所以第一个玩家将会输掉比赛。
66+
1. 当 4ドル \lt n \lt 8$ 时,即 $n = 5, 6, 7,ドル第一个玩家可以相应地将数字减少为 4ドル,ドル那么 4ドル$ 这个死亡数字给到了第二个玩家,第二个玩家将会输掉比赛。
67+
1. 当 $n = 8,ドル无论第一个玩家选择 1,ドル 2, 3$ 哪个数字,都会把 4ドル \lt n \lt 8$ 的数字留给第二个,所以第一个玩家将会输掉比赛。
6768
1. ...
6869
1. 依次类推,当玩家拿到 $n$ 这个数字,且 $n$ 能被 4ドル$ 整除,他将会输掉比赛,否则他将赢得比赛。
6970

70-
时间复杂度 $O(1)$。
71+
时间复杂度 $O(1)$,空间复杂度 $O(1)$
7172

7273
<!-- tabs:start -->
7374

‎solution/0200-0299/0292.Nim Game/README_EN.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,20 @@ In all outcomes, your friend wins.
5151

5252
## Solutions
5353

54-
### Solution 1
54+
### Solution 1: Finding the Pattern
55+
56+
The first player who gets a multiple of 4ドル$ (i.e., $n$ can be divided by 4ドル$) will lose the game.
57+
58+
Proof:
59+
60+
1. When $n \lt 4,ドル the first player can directly take all the stones, so the first player will win the game.
61+
1. When $n = 4,ドル no matter whether the first player chooses 1,ドル 2, 3,ドル the second player can always choose the remaining number, so the first player will lose the game.
62+
1. When 4ドル \lt n \lt 8,ドル i.e., $n = 5, 6, 7,ドル the first player can correspondingly reduce the number to 4ドル,ドル then the "death number" 4ドル$ is given to the second player, and the second player will lose the game.
63+
1. When $n = 8,ドル no matter whether the first player chooses 1,ドル 2, 3,ドル it will leave a number between 4ドル \lt n \lt 8$ to the second player, so the first player will lose the game.
64+
1. ...
65+
1. By induction, when a player gets the number $n,ドル and $n$ can be divided by 4ドル,ドル he will lose the game, otherwise, he will win the game.
66+
67+
The time complexity is $O(1),ドル and the space complexity is $O(1)$.
5568

5669
<!-- tabs:start -->
5770

‎solution/0200-0299/0293.Flip Game/README.md‎

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@
3939

4040
## 解法
4141

42-
### 方法一
42+
### 方法一:遍历 + 模拟
43+
44+
我们遍历字符串,如果当前字符和下一个字符都是 `+`,那么我们就将这两个字符变成 `-`,然后将结果加入到结果数组中,再将这两个字符变回 `+`
45+
46+
遍历结束后,返回结果数组即可。
47+
48+
时间复杂度 $O(n^2),ドル其中 $n$ 是字符串长度。忽略答案数组的空间复杂度,空间复杂度 $O(n)$ 或 $O(1)$。
4349

4450
<!-- tabs:start -->
4551

@@ -48,8 +54,8 @@ class Solution:
4854
def generatePossibleNextMoves(self, currentState: str) -> List[str]:
4955
s = list(currentState)
5056
ans = []
51-
for i, c in enumerate(s[:-1]):
52-
if c == "+"and s[i +1] == "+":
57+
for i, (a, b) in enumerate(pairwise(s)):
58+
if a == b == "+":
5359
s[i] = s[i + 1] = "-"
5460
ans.append("".join(s))
5561
s[i] = s[i + 1] = "+"
@@ -59,15 +65,15 @@ class Solution:
5965
```java
6066
class Solution {
6167
public List<String> generatePossibleNextMoves(String currentState) {
62-
char[] cs = currentState.toCharArray();
6368
List<String> ans = new ArrayList<>();
64-
for (int i = 0; i < cs.length - 1; ++i) {
65-
if (cs[i] == '+' && cs[i + 1] == '+') {
66-
cs[i] = '-';
67-
cs[i + 1] = '-';
68-
ans.add(String.valueOf(cs));
69-
cs[i] = '+';
70-
cs[i + 1] = '+';
69+
char[] s = currentState.toCharArray();
70+
for (int i = 0; i < s.length - 1; ++i) {
71+
if (s[i] == '+' && s[i + 1] == '+') {
72+
s[i] = '-';
73+
s[i + 1] = '-';
74+
ans.add(new String(s));
75+
s[i] = '+';
76+
s[i + 1] = '+';
7177
}
7278
}
7379
return ans;
@@ -78,15 +84,13 @@ class Solution {
7884
```cpp
7985
class Solution {
8086
public:
81-
vector<string> generatePossibleNextMoves(string currentState) {
87+
vector<string> generatePossibleNextMoves(string s) {
8288
vector<string> ans;
83-
for (int i = 0; i < currentState.size() - 1; ++i) {
84-
if (currentState[i] == '+' && currentState[i + 1] == '+') {
85-
currentState[i] = '-';
86-
currentState[i + 1] = '-';
87-
ans.push_back(currentState);
88-
currentState[i] = '+';
89-
currentState[i + 1] = '+';
89+
for (int i = 0; i < s.size() - 1; ++i) {
90+
if (s[i] == '+' && s[i + 1] == '+') {
91+
s[i] = s[i + 1] = '-';
92+
ans.emplace_back(s);
93+
s[i] = s[i + 1] = '+';
9094
}
9195
}
9296
return ans;
@@ -95,17 +99,31 @@ public:
9599
```
96100
97101
```go
98-
func generatePossibleNextMoves(currentState string) []string {
99-
ans := []string{}
100-
cs := []byte(currentState)
101-
for i, c := range cs[1:] {
102-
if c == '+' && cs[i] == '+' {
103-
cs[i], cs[i+1] = '-', '-'
104-
ans = append(ans, string(cs))
105-
cs[i], cs[i+1] = '+', '+'
102+
func generatePossibleNextMoves(currentState string) (ans []string) {
103+
s := []byte(currentState)
104+
for i := 0; i < len(s)-1; i++ {
105+
if s[i] == '+' && s[i+1] == '+' {
106+
s[i], s[i+1] = '-', '-'
107+
ans = append(ans, string(s))
108+
s[i], s[i+1] = '+', '+'
106109
}
107110
}
108-
return ans
111+
return
112+
}
113+
```
114+
115+
```ts
116+
function generatePossibleNextMoves(currentState: string): string[] {
117+
const s = currentState.split('');
118+
const ans: string[] = [];
119+
for (let i = 0; i < s.length - 1; ++i) {
120+
if (s[i] === '+' && s[i + 1] === '+') {
121+
s[i] = s[i + 1] = '-';
122+
ans.push(s.join(''));
123+
s[i] = s[i + 1] = '+';
124+
}
125+
}
126+
return ans;
109127
}
110128
```
111129

‎solution/0200-0299/0293.Flip Game/README_EN.md‎

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@
3535

3636
## Solutions
3737

38-
### Solution 1
38+
### Solution 1: Traversal + Simulation
39+
40+
We traverse the string. If the current character and the next character are both `+`, we change these two characters to `-`, add the result to the result array, and then change these two characters back to `+`.
41+
42+
After the traversal ends, we return the result array.
43+
44+
The time complexity is $O(n^2),ドル where $n$ is the length of the string. Ignoring the space complexity of the result array, the space complexity is $O(n)$ or $O(1)$.
3945

4046
<!-- tabs:start -->
4147

@@ -44,8 +50,8 @@ class Solution:
4450
def generatePossibleNextMoves(self, currentState: str) -> List[str]:
4551
s = list(currentState)
4652
ans = []
47-
for i, c in enumerate(s[:-1]):
48-
if c == "+"and s[i +1] == "+":
53+
for i, (a, b) in enumerate(pairwise(s)):
54+
if a == b == "+":
4955
s[i] = s[i + 1] = "-"
5056
ans.append("".join(s))
5157
s[i] = s[i + 1] = "+"
@@ -55,15 +61,15 @@ class Solution:
5561
```java
5662
class Solution {
5763
public List<String> generatePossibleNextMoves(String currentState) {
58-
char[] cs = currentState.toCharArray();
5964
List<String> ans = new ArrayList<>();
60-
for (int i = 0; i < cs.length - 1; ++i) {
61-
if (cs[i] == '+' && cs[i + 1] == '+') {
62-
cs[i] = '-';
63-
cs[i + 1] = '-';
64-
ans.add(String.valueOf(cs));
65-
cs[i] = '+';
66-
cs[i + 1] = '+';
65+
char[] s = currentState.toCharArray();
66+
for (int i = 0; i < s.length - 1; ++i) {
67+
if (s[i] == '+' && s[i + 1] == '+') {
68+
s[i] = '-';
69+
s[i + 1] = '-';
70+
ans.add(new String(s));
71+
s[i] = '+';
72+
s[i + 1] = '+';
6773
}
6874
}
6975
return ans;
@@ -74,15 +80,13 @@ class Solution {
7480
```cpp
7581
class Solution {
7682
public:
77-
vector<string> generatePossibleNextMoves(string currentState) {
83+
vector<string> generatePossibleNextMoves(string s) {
7884
vector<string> ans;
79-
for (int i = 0; i < currentState.size() - 1; ++i) {
80-
if (currentState[i] == '+' && currentState[i + 1] == '+') {
81-
currentState[i] = '-';
82-
currentState[i + 1] = '-';
83-
ans.push_back(currentState);
84-
currentState[i] = '+';
85-
currentState[i + 1] = '+';
85+
for (int i = 0; i < s.size() - 1; ++i) {
86+
if (s[i] == '+' && s[i + 1] == '+') {
87+
s[i] = s[i + 1] = '-';
88+
ans.emplace_back(s);
89+
s[i] = s[i + 1] = '+';
8690
}
8791
}
8892
return ans;
@@ -91,17 +95,31 @@ public:
9195
```
9296
9397
```go
94-
func generatePossibleNextMoves(currentState string) []string {
95-
ans := []string{}
96-
cs := []byte(currentState)
97-
for i, c := range cs[1:] {
98-
if c == '+' && cs[i] == '+' {
99-
cs[i], cs[i+1] = '-', '-'
100-
ans = append(ans, string(cs))
101-
cs[i], cs[i+1] = '+', '+'
98+
func generatePossibleNextMoves(currentState string) (ans []string) {
99+
s := []byte(currentState)
100+
for i := 0; i < len(s)-1; i++ {
101+
if s[i] == '+' && s[i+1] == '+' {
102+
s[i], s[i+1] = '-', '-'
103+
ans = append(ans, string(s))
104+
s[i], s[i+1] = '+', '+'
102105
}
103106
}
104-
return ans
107+
return
108+
}
109+
```
110+
111+
```ts
112+
function generatePossibleNextMoves(currentState: string): string[] {
113+
const s = currentState.split('');
114+
const ans: string[] = [];
115+
for (let i = 0; i < s.length - 1; ++i) {
116+
if (s[i] === '+' && s[i + 1] === '+') {
117+
s[i] = s[i + 1] = '-';
118+
ans.push(s.join(''));
119+
s[i] = s[i + 1] = '+';
120+
}
121+
}
122+
return ans;
105123
}
106124
```
107125

‎solution/0200-0299/0293.Flip Game/Solution.cpp‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Solution {
22
public:
3-
vector<string> generatePossibleNextMoves(string currentState) {
3+
vector<string> generatePossibleNextMoves(string s) {
44
vector<string> ans;
5-
for (int i = 0; i < currentState.size() - 1; ++i) {
6-
if (currentState[i] == '+' && currentState[i + 1] == '+') {
7-
currentState[i] = '-';
8-
currentState[i + 1] = '-';
9-
ans.push_back(currentState);
10-
currentState[i] = '+';
11-
currentState[i + 1] = '+';
5+
for (int i = 0; i < s.size() - 1; ++i) {
6+
if (s[i] == '+' && s[i + 1] == '+') {
7+
s[i] = s[i + 1] = '-';
8+
ans.emplace_back(s);
9+
s[i] = s[i + 1] = '+';
1210
}
1311
}
1412
return ans;
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
func generatePossibleNextMoves(currentState string) []string {
2-
ans := []string{}
3-
cs := []byte(currentState)
4-
for i, c := range cs[1:] {
5-
if c == '+' && cs[i] == '+' {
6-
cs[i], cs[i+1] = '-', '-'
7-
ans = append(ans, string(cs))
8-
cs[i], cs[i+1] = '+', '+'
1+
func generatePossibleNextMoves(currentState string) (ans []string) {
2+
s := []byte(currentState)
3+
for i := 0; i < len(s)-1; i++ {
4+
if s[i] == '+' && s[i+1] == '+' {
5+
s[i], s[i+1] = '-', '-'
6+
ans = append(ans, string(s))
7+
s[i], s[i+1] = '+', '+'
98
}
109
}
11-
returnans
10+
return
1211
}

‎solution/0200-0299/0293.Flip Game/Solution.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public List<String> generatePossibleNextMoves(String currentState) {
3-
char[] cs = currentState.toCharArray();
43
List<String> ans = new ArrayList<>();
5-
for (int i = 0; i < cs.length - 1; ++i) {
6-
if (cs[i] == '+' && cs[i + 1] == '+') {
7-
cs[i] = '-';
8-
cs[i + 1] = '-';
9-
ans.add(String.valueOf(cs));
10-
cs[i] = '+';
11-
cs[i + 1] = '+';
4+
char[] s = currentState.toCharArray();
5+
for (int i = 0; i < s.length - 1; ++i) {
6+
if (s[i] == '+' && s[i + 1] == '+') {
7+
s[i] = '-';
8+
s[i + 1] = '-';
9+
ans.add(new String(s));
10+
s[i] = '+';
11+
s[i + 1] = '+';
1212
}
1313
}
1414
return ans;

‎solution/0200-0299/0293.Flip Game/Solution.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class Solution:
22
def generatePossibleNextMoves(self, currentState: str) -> List[str]:
33
s = list(currentState)
44
ans = []
5-
for i, cin enumerate(s[:-1]):
6-
if c == "+"ands[i+1] == "+":
5+
for i, (a, b) in enumerate(pairwise(s)):
6+
if a == b == "+":
77
s[i] = s[i + 1] = "-"
88
ans.append("".join(s))
99
s[i] = s[i + 1] = "+"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function generatePossibleNextMoves(currentState: string): string[] {
2+
const s = currentState.split('');
3+
const ans: string[] = [];
4+
for (let i = 0; i < s.length - 1; ++i) {
5+
if (s[i] === '+' && s[i + 1] === '+') {
6+
s[i] = s[i + 1] = '-';
7+
ans.push(s.join(''));
8+
s[i] = s[i + 1] = '+';
9+
}
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
(0)

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