diff --git a/solution/0200-0299/0292.Nim Game/README.md b/solution/0200-0299/0292.Nim Game/README.md index 58472ad2ca03b..093819b4e5b6a 100644 --- a/solution/0200-0299/0292.Nim Game/README.md +++ b/solution/0200-0299/0292.Nim Game/README.md @@ -55,19 +55,20 @@ ## 解法 -### 方法一:数学推理 +### 方法一:找规律 第一个得到 $4$ 的倍数(即 $n$ 能被 $4$ 整除)的将会输掉比赛。 证明: -1. 当 $n=4$,无论第一个玩家选择 $1/2/3$ 哪个数字,第二个玩家总能选择剩下的数字,**第一个玩家将会输掉比赛**。 -1. 当 $4 diff --git a/solution/0200-0299/0292.Nim Game/README_EN.md b/solution/0200-0299/0292.Nim Game/README_EN.md index 0ad5a6be63b81..b69037519e157 100644 --- a/solution/0200-0299/0292.Nim Game/README_EN.md +++ b/solution/0200-0299/0292.Nim Game/README_EN.md @@ -51,7 +51,20 @@ In all outcomes, your friend wins. ## Solutions -### Solution 1 +### Solution 1: Finding the Pattern + +The first player who gets a multiple of $4$ (i.e., $n$ can be divided by $4$) will lose the game. + +Proof: + +1. When $n \lt 4$, the first player can directly take all the stones, so the first player will win the game. +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. +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. +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. +1. ... +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. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. diff --git a/solution/0200-0299/0293.Flip Game/README.md b/solution/0200-0299/0293.Flip Game/README.md index ee05f848eb4e1..47f4200bcb94f 100644 --- a/solution/0200-0299/0293.Flip Game/README.md +++ b/solution/0200-0299/0293.Flip Game/README.md @@ -39,7 +39,13 @@ ## 解法 -### 方法一 +### 方法一:遍历 + 模拟 + +我们遍历字符串,如果当前字符和下一个字符都是 `+`,那么我们就将这两个字符变成 `-`,然后将结果加入到结果数组中,再将这两个字符变回 `+`。 + +遍历结束后,返回结果数组即可。 + +时间复杂度 $O(n^2)$,其中 $n$ 是字符串长度。忽略答案数组的空间复杂度,空间复杂度 $O(n)$ 或 $O(1)$。 @@ -48,8 +54,8 @@ class Solution: def generatePossibleNextMoves(self, currentState: str) -> List[str]: s = list(currentState) ans = [] - for i, c in enumerate(s[:-1]): - if c == "+" and s[i + 1] == "+": + for i, (a, b) in enumerate(pairwise(s)): + if a == b == "+": s[i] = s[i + 1] = "-" ans.append("".join(s)) s[i] = s[i + 1] = "+" @@ -59,15 +65,15 @@ class Solution: ```java class Solution { public List generatePossibleNextMoves(String currentState) { - char[] cs = currentState.toCharArray(); List ans = new ArrayList<>(); - for (int i = 0; i < cs.length - 1; ++i) { - if (cs[i] == '+' && cs[i + 1] == '+') { - cs[i] = '-'; - cs[i + 1] = '-'; - ans.add(String.valueOf(cs)); - cs[i] = '+'; - cs[i + 1] = '+'; + char[] s = currentState.toCharArray(); + for (int i = 0; i < s.length - 1; ++i) { + if (s[i] == '+' && s[i + 1] == '+') { + s[i] = '-'; + s[i + 1] = '-'; + ans.add(new String(s)); + s[i] = '+'; + s[i + 1] = '+'; } } return ans; @@ -78,15 +84,13 @@ class Solution { ```cpp class Solution { public: - vector generatePossibleNextMoves(string currentState) { + vector generatePossibleNextMoves(string s) { vector ans; - for (int i = 0; i < currentState.size() - 1; ++i) { - if (currentState[i] == '+' && currentState[i + 1] == '+') { - currentState[i] = '-'; - currentState[i + 1] = '-'; - ans.push_back(currentState); - currentState[i] = '+'; - currentState[i + 1] = '+'; + for (int i = 0; i < s.size() - 1; ++i) { + if (s[i] == '+' && s[i + 1] == '+') { + s[i] = s[i + 1] = '-'; + ans.emplace_back(s); + s[i] = s[i + 1] = '+'; } } return ans; @@ -95,17 +99,31 @@ public: ``` ```go -func generatePossibleNextMoves(currentState string) []string { - ans := []string{} - cs := []byte(currentState) - for i, c := range cs[1:] { - if c == '+' && cs[i] == '+' { - cs[i], cs[i+1] = '-', '-' - ans = append(ans, string(cs)) - cs[i], cs[i+1] = '+', '+' +func generatePossibleNextMoves(currentState string) (ans []string) { + s := []byte(currentState) + for i := 0; i < len(s)-1; i++ { + if s[i] == '+' && s[i+1] == '+' { + s[i], s[i+1] = '-', '-' + ans = append(ans, string(s)) + s[i], s[i+1] = '+', '+' } } - return ans + return +} +``` + +```ts +function generatePossibleNextMoves(currentState: string): string[] { + const s = currentState.split(''); + const ans: string[] = []; + for (let i = 0; i < s.length - 1; ++i) { + if (s[i] === '+' && s[i + 1] === '+') { + s[i] = s[i + 1] = '-'; + ans.push(s.join('')); + s[i] = s[i + 1] = '+'; + } + } + return ans; } ``` diff --git a/solution/0200-0299/0293.Flip Game/README_EN.md b/solution/0200-0299/0293.Flip Game/README_EN.md index c87394b843239..9287146803058 100644 --- a/solution/0200-0299/0293.Flip Game/README_EN.md +++ b/solution/0200-0299/0293.Flip Game/README_EN.md @@ -35,7 +35,13 @@ ## Solutions -### Solution 1 +### Solution 1: Traversal + Simulation + +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 `+`. + +After the traversal ends, we return the result array. + +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)$. @@ -44,8 +50,8 @@ class Solution: def generatePossibleNextMoves(self, currentState: str) -> List[str]: s = list(currentState) ans = [] - for i, c in enumerate(s[:-1]): - if c == "+" and s[i + 1] == "+": + for i, (a, b) in enumerate(pairwise(s)): + if a == b == "+": s[i] = s[i + 1] = "-" ans.append("".join(s)) s[i] = s[i + 1] = "+" @@ -55,15 +61,15 @@ class Solution: ```java class Solution { public List generatePossibleNextMoves(String currentState) { - char[] cs = currentState.toCharArray(); List ans = new ArrayList<>(); - for (int i = 0; i < cs.length - 1; ++i) { - if (cs[i] == '+' && cs[i + 1] == '+') { - cs[i] = '-'; - cs[i + 1] = '-'; - ans.add(String.valueOf(cs)); - cs[i] = '+'; - cs[i + 1] = '+'; + char[] s = currentState.toCharArray(); + for (int i = 0; i < s.length - 1; ++i) { + if (s[i] == '+' && s[i + 1] == '+') { + s[i] = '-'; + s[i + 1] = '-'; + ans.add(new String(s)); + s[i] = '+'; + s[i + 1] = '+'; } } return ans; @@ -74,15 +80,13 @@ class Solution { ```cpp class Solution { public: - vector generatePossibleNextMoves(string currentState) { + vector generatePossibleNextMoves(string s) { vector ans; - for (int i = 0; i < currentState.size() - 1; ++i) { - if (currentState[i] == '+' && currentState[i + 1] == '+') { - currentState[i] = '-'; - currentState[i + 1] = '-'; - ans.push_back(currentState); - currentState[i] = '+'; - currentState[i + 1] = '+'; + for (int i = 0; i < s.size() - 1; ++i) { + if (s[i] == '+' && s[i + 1] == '+') { + s[i] = s[i + 1] = '-'; + ans.emplace_back(s); + s[i] = s[i + 1] = '+'; } } return ans; @@ -91,17 +95,31 @@ public: ``` ```go -func generatePossibleNextMoves(currentState string) []string { - ans := []string{} - cs := []byte(currentState) - for i, c := range cs[1:] { - if c == '+' && cs[i] == '+' { - cs[i], cs[i+1] = '-', '-' - ans = append(ans, string(cs)) - cs[i], cs[i+1] = '+', '+' +func generatePossibleNextMoves(currentState string) (ans []string) { + s := []byte(currentState) + for i := 0; i < len(s)-1; i++ { + if s[i] == '+' && s[i+1] == '+' { + s[i], s[i+1] = '-', '-' + ans = append(ans, string(s)) + s[i], s[i+1] = '+', '+' } } - return ans + return +} +``` + +```ts +function generatePossibleNextMoves(currentState: string): string[] { + const s = currentState.split(''); + const ans: string[] = []; + for (let i = 0; i < s.length - 1; ++i) { + if (s[i] === '+' && s[i + 1] === '+') { + s[i] = s[i + 1] = '-'; + ans.push(s.join('')); + s[i] = s[i + 1] = '+'; + } + } + return ans; } ``` diff --git a/solution/0200-0299/0293.Flip Game/Solution.cpp b/solution/0200-0299/0293.Flip Game/Solution.cpp index 3f2a37dc6e91e..e038783ed38b1 100644 --- a/solution/0200-0299/0293.Flip Game/Solution.cpp +++ b/solution/0200-0299/0293.Flip Game/Solution.cpp @@ -1,14 +1,12 @@ class Solution { public: - vector generatePossibleNextMoves(string currentState) { + vector generatePossibleNextMoves(string s) { vector ans; - for (int i = 0; i < currentState.size() - 1; ++i) { - if (currentState[i] == '+' && currentState[i + 1] == '+') { - currentState[i] = '-'; - currentState[i + 1] = '-'; - ans.push_back(currentState); - currentState[i] = '+'; - currentState[i + 1] = '+'; + for (int i = 0; i < s.size() - 1; ++i) { + if (s[i] == '+' && s[i + 1] == '+') { + s[i] = s[i + 1] = '-'; + ans.emplace_back(s); + s[i] = s[i + 1] = '+'; } } return ans; diff --git a/solution/0200-0299/0293.Flip Game/Solution.go b/solution/0200-0299/0293.Flip Game/Solution.go index 53e6461df0f3a..4538bb71ec459 100644 --- a/solution/0200-0299/0293.Flip Game/Solution.go +++ b/solution/0200-0299/0293.Flip Game/Solution.go @@ -1,12 +1,11 @@ -func generatePossibleNextMoves(currentState string) []string { - ans := []string{} - cs := []byte(currentState) - for i, c := range cs[1:] { - if c == '+' && cs[i] == '+' { - cs[i], cs[i+1] = '-', '-' - ans = append(ans, string(cs)) - cs[i], cs[i+1] = '+', '+' +func generatePossibleNextMoves(currentState string) (ans []string) { + s := []byte(currentState) + for i := 0; i < len(s)-1; i++ { + if s[i] == '+' && s[i+1] == '+' { + s[i], s[i+1] = '-', '-' + ans = append(ans, string(s)) + s[i], s[i+1] = '+', '+' } } - return ans + return } \ No newline at end of file diff --git a/solution/0200-0299/0293.Flip Game/Solution.java b/solution/0200-0299/0293.Flip Game/Solution.java index 29a103f0d3c27..56fdb45d46d8e 100644 --- a/solution/0200-0299/0293.Flip Game/Solution.java +++ b/solution/0200-0299/0293.Flip Game/Solution.java @@ -1,14 +1,14 @@ class Solution { public List generatePossibleNextMoves(String currentState) { - char[] cs = currentState.toCharArray(); List ans = new ArrayList<>(); - for (int i = 0; i < cs.length - 1; ++i) { - if (cs[i] == '+' && cs[i + 1] == '+') { - cs[i] = '-'; - cs[i + 1] = '-'; - ans.add(String.valueOf(cs)); - cs[i] = '+'; - cs[i + 1] = '+'; + char[] s = currentState.toCharArray(); + for (int i = 0; i < s.length - 1; ++i) { + if (s[i] == '+' && s[i + 1] == '+') { + s[i] = '-'; + s[i + 1] = '-'; + ans.add(new String(s)); + s[i] = '+'; + s[i + 1] = '+'; } } return ans; diff --git a/solution/0200-0299/0293.Flip Game/Solution.py b/solution/0200-0299/0293.Flip Game/Solution.py index 10dabe983f912..9204d037b13cb 100644 --- a/solution/0200-0299/0293.Flip Game/Solution.py +++ b/solution/0200-0299/0293.Flip Game/Solution.py @@ -2,8 +2,8 @@ class Solution: def generatePossibleNextMoves(self, currentState: str) -> List[str]: s = list(currentState) ans = [] - for i, c in enumerate(s[:-1]): - if c == "+" and s[i + 1] == "+": + for i, (a, b) in enumerate(pairwise(s)): + if a == b == "+": s[i] = s[i + 1] = "-" ans.append("".join(s)) s[i] = s[i + 1] = "+" diff --git a/solution/0200-0299/0293.Flip Game/Solution.ts b/solution/0200-0299/0293.Flip Game/Solution.ts new file mode 100644 index 0000000000000..49d7fcf8d91dd --- /dev/null +++ b/solution/0200-0299/0293.Flip Game/Solution.ts @@ -0,0 +1,12 @@ +function generatePossibleNextMoves(currentState: string): string[] { + const s = currentState.split(''); + const ans: string[] = []; + for (let i = 0; i < s.length - 1; ++i) { + if (s[i] === '+' && s[i + 1] === '+') { + s[i] = s[i + 1] = '-'; + ans.push(s.join('')); + s[i] = s[i + 1] = '+'; + } + } + return ans; +}