Skip to content

Commit 9dbefba

Browse files
committed
Add solution 0810
1 parent 991cb6e commit 9dbefba

File tree

12 files changed

+232
-52
lines changed

12 files changed

+232
-52
lines changed

README.md

Lines changed: 44 additions & 44 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode
2+
3+
func xorGame(nums []int) bool {
4+
if len(nums)%2 == 0 {
5+
return true
6+
}
7+
xor := 0
8+
for _, num := range nums {
9+
xor ^= num
10+
}
11+
return xor == 0
12+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question810 struct {
9+
para810
10+
ans810
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para810 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans810 struct {
22+
one bool
23+
}
24+
25+
func Test_Problem810(t *testing.T) {
26+
27+
qs := []question810{
28+
29+
{
30+
para810{[]int{1, 1, 2}},
31+
ans810{false},
32+
},
33+
}
34+
35+
fmt.Printf("------------------------Leetcode Problem 810------------------------\n")
36+
37+
for _, q := range qs {
38+
_, p := q.ans810, q.para810
39+
fmt.Printf("【input】:%v 【output】:%v\n", p, xorGame(p.nums))
40+
}
41+
fmt.Printf("\n\n\n")
42+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# [810. Chalkboard XOR Game](https://leetcode.com/problems/chalkboard-xor-game/)
2+
3+
4+
## 题目
5+
6+
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.)
7+
8+
Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins.
9+
10+
Return True if and only if Alice wins the game, assuming both players play optimally.
11+
12+
```
13+
Example:Input: nums = [1, 1, 2]
14+
Output: false
15+
Explanation:
16+
Alice has two choices: erase 1 or erase 2.
17+
If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose.
18+
If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose.
19+
```
20+
21+
**Notes:**
22+
23+
- `1 <= N <= 1000`.
24+
- `0 <= nums[i] <= 2^16`.
25+
26+
## 题目大意
27+
28+
黑板上写着一个非负整数数组 nums[i] 。Alice 和 Bob 轮流从黑板上擦掉一个数字,Alice 先手。如果擦除一个数字后,剩余的所有数字按位异或运算得出的结果等于 0 的话,当前玩家游戏失败。 (另外,如果只剩一个数字,按位异或运算得到它本身;如果无数字剩余,按位异或运算结果为 0。)并且,轮到某个玩家时,如果当前黑板上所有数字按位异或运算结果等于 0,这个玩家获胜。假设两个玩家每步都使用最优解,当且仅当 Alice 获胜时返回 true。
29+
30+
## 解题思路
31+
32+
- Alice 必胜情况之一,Alice 先手,起始数组全部元素本身异或结果就为 0 。不需要擦除数字便自动获胜。除去这个情况,还有其他情况么?由于 2 人是交替擦除数字,且每次都恰好擦掉一个数字,因此对于这两人中的任意一人,其每次在擦除数字前,黑板上剩余数字的个数的奇偶性一定都是相同的。于是奇偶性成为突破口。
33+
- 如果 nums 的长度是偶数,Alice 先手是否必败呢?如果必败,代表无论擦掉哪一个数字,剩余所有数字的异或结果都等于 0。利用反证法证明上述结论是错误的。首先 $num[0] \oplus num[1] \oplus num[2] \oplus \cdots \oplus num[n-1] = X ≠ 0$,初始所有元素异或结果不为 0。假设 Alice 当前擦掉第 i 个元素,0 ≤ i < n。令 $X_{n}$ 代表擦掉第 n 位元素以后剩余元素异或的结果。由证题,无论擦掉哪一个数字,剩余所有数字的异或结果都等于 0。所以 $X_{0} \oplus X_{1} \oplus X_{2} \oplus \cdots  \oplus X_{n-1} = 0$。
34+
35+
$$\begin{aligned}0 &= X_{0} \oplus  X_{1} \oplus X_{2} \oplus \cdots  \oplus X_{n-1} \\0 &= (X \oplus nums[0]) \oplus (X \oplus nums[1]) \oplus (X \oplus nums[2]) \oplus \cdots  \oplus (X \oplus nums[n-1])\\ 0 &= (X \oplus X \oplus \cdots  \oplus X) \oplus (nums[0] \oplus nums[1] \oplus nums[2] \oplus \cdots  \oplus nums[n-1])\\0 &= 0 \oplus X\\\\\Rightarrow X &= 0\\\end{aligned}$$
36+
37+
由于 n 为偶数,所以 n 个 X 的异或结果为 0。最终推出 X = 0,很明显与前提 X ≠ 0 冲突。所以原命题,代表无论擦掉哪一个数字,剩余所有数字的异或结果都等于 0 是错误的。也就是说,当 n 为偶数时,代表无论擦掉哪一个数字,剩余所有数字的异或结果都不等于 0。即 Alice 有必胜策略。换句话说,当数组的长度是偶数时,先手 Alice 总能找到一个数字,在擦掉这个数字之后剩余的所有数字异或结果不等于 0。
38+
39+
- 综上,Alice 必胜策略有 2 种情况:
40+
1. 数组 nums 的全部元素初始本身异或结果就等于 0。
41+
2. 数组 nums 的长度是偶数。
42+
43+
## 代码
44+
45+
```go
46+
package leetcode
47+
48+
func xorGame(nums []int) bool {
49+
if len(nums)%2 == 0 {
50+
return true
51+
}
52+
xor := 0
53+
for _, num := range nums {
54+
xor ^= num
55+
}
56+
return xor == 0
57+
}
58+
```

website/content/ChapterFour/0800~0899/0803.Bricks-Falling-When-Hit.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,5 @@ func getUnionFindFromGrid(grid [][]int, x, y int, uf template.UnionFindCount) {
128128
----------------------------------------------
129129
<div style="display: flex;justify-content: space-between;align-items: center;">
130130
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0802.Find-Eventual-Safe-States/">⬅️上一页</a></p>
131-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0811.Subdomain-Visit-Count/">下一页➡️</a></p>
131+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0810.Chalkboard-XOR-Game/">下一页➡️</a></p>
132132
</div>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [810. Chalkboard XOR Game](https://leetcode.com/problems/chalkboard-xor-game/)
2+
3+
4+
## 题目
5+
6+
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.)
7+
8+
Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins.
9+
10+
Return True if and only if Alice wins the game, assuming both players play optimally.
11+
12+
```
13+
Example:Input: nums = [1, 1, 2]
14+
Output: false
15+
Explanation:
16+
Alice has two choices: erase 1 or erase 2.
17+
If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose.
18+
If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose.
19+
```
20+
21+
**Notes:**
22+
23+
- `1 <= N <= 1000`.
24+
- `0 <= nums[i] <= 2^16`.
25+
26+
## 题目大意
27+
28+
黑板上写着一个非负整数数组 nums[i] 。Alice 和 Bob 轮流从黑板上擦掉一个数字,Alice 先手。如果擦除一个数字后,剩余的所有数字按位异或运算得出的结果等于 0 的话,当前玩家游戏失败。 (另外,如果只剩一个数字,按位异或运算得到它本身;如果无数字剩余,按位异或运算结果为 0。)并且,轮到某个玩家时,如果当前黑板上所有数字按位异或运算结果等于 0,这个玩家获胜。假设两个玩家每步都使用最优解,当且仅当 Alice 获胜时返回 true。
29+
30+
## 解题思路
31+
32+
- Alice 必胜情况之一,Alice 先手,起始数组全部元素本身异或结果就为 0 。不需要擦除数字便自动获胜。除去这个情况,还有其他情况么?由于 2 人是交替擦除数字,且每次都恰好擦掉一个数字,因此对于这两人中的任意一人,其每次在擦除数字前,黑板上剩余数字的个数的奇偶性一定都是相同的。于是奇偶性成为突破口。
33+
- 如果 nums 的长度是偶数,Alice 先手是否必败呢?如果必败,代表无论擦掉哪一个数字,剩余所有数字的异或结果都等于 0。利用反证法证明上述结论是错误的。首先 {{< katex >}} num[0] \oplus num[1] \oplus num[2] \oplus \cdots  \oplus num[n-1] = X ≠ 0 {{< /katex >}} ,初始所有元素异或结果不为 0。假设 Alice 当前擦掉第 i 个元素,0 ≤ i < n。令 {{< katex >}}X_{n}{{< /katex >}} 代表擦掉第 n 位元素以后剩余元素异或的结果。由证题,无论擦掉哪一个数字,剩余所有数字的异或结果都等于 0。所以 {{< katex >}} X_{0} \oplus X_{1} \oplus X_{2} \oplus \cdots  \oplus X_{n-1} = 0{{< /katex >}} 。
34+
35+
{{< katex display >}}
36+
\begin{aligned}0 &= X_{0} \oplus  X_{1} \oplus X_{2} \oplus \cdots  \oplus X_{n-1} \\0 &= (X \oplus nums[0]) \oplus (X \oplus nums[1]) \oplus (X \oplus nums[2]) \oplus \cdots  \oplus (X \oplus nums[n-1])\\ 0 &= (X \oplus X \oplus \cdots  \oplus X) \oplus (nums[0] \oplus nums[1] \oplus nums[2] \oplus \cdots  \oplus nums[n-1])\\0 &= 0 \oplus X\\\\\Rightarrow X &= 0\\\end{aligned}
37+
{{< /katex >}}
38+
39+
由于 n 为偶数,所以 n 个 X 的异或结果为 0。最终推出 X = 0,很明显与前提 X ≠ 0 冲突。所以原命题,代表无论擦掉哪一个数字,剩余所有数字的异或结果都等于 0 是错误的。也就是说,当 n 为偶数时,代表无论擦掉哪一个数字,剩余所有数字的异或结果都不等于 0。即 Alice 有必胜策略。换句话说,当数组的长度是偶数时,先手 Alice 总能找到一个数字,在擦掉这个数字之后剩余的所有数字异或结果不等于 0。
40+
41+
- 综上,Alice 必胜策略有 2 种情况:
42+
1. 数组 nums 的全部元素初始本身异或结果就等于 0。
43+
2. 数组 nums 的长度是偶数。
44+
45+
## 代码
46+
47+
```go
48+
package leetcode
49+
50+
func xorGame(nums []int) bool {
51+
if len(nums)%2 == 0 {
52+
return true
53+
}
54+
xor := 0
55+
for _, num := range nums {
56+
xor ^= num
57+
}
58+
return xor == 0
59+
}
60+
```
61+
62+
63+
----------------------------------------------
64+
<div style="display: flex;justify-content: space-between;align-items: center;">
65+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0803.Bricks-Falling-When-Hit/">⬅️上一页</a></p>
66+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0811.Subdomain-Visit-Count/">下一页➡️</a></p>
67+
</div>

website/content/ChapterFour/0800~0899/0811.Subdomain-Visit-Count.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,6 @@ func splitDomain(domain string, domains map[string]int) {
145145

146146
----------------------------------------------
147147
<div style="display: flex;justify-content: space-between;align-items: center;">
148-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0803.Bricks-Falling-When-Hit/">⬅️上一页</a></p>
148+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0810.Chalkboard-XOR-Game/">⬅️上一页</a></p>
149149
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0812.Largest-Triangle-Area/">下一页➡️</a></p>
150150
</div>

website/content/ChapterTwo/Array.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ weight: 1
169169
|1636|Sort Array by Increasing Frequency|[Go]({{< relref "/ChapterFour/1600~1699/1636.Sort-Array-by-Increasing-Frequency.md" >}})|Easy||||67.1%|
170170
|1640|Check Array Formation Through Concatenation|[Go]({{< relref "/ChapterFour/1600~1699/1640.Check-Array-Formation-Through-Concatenation.md" >}})|Easy||||59.6%|
171171
|1646|Get Maximum in Generated Array|[Go]({{< relref "/ChapterFour/1600~1699/1646.Get-Maximum-in-Generated-Array.md" >}})|Easy||||53.0%|
172-
|1652|Defuse the Bomb|[Go]({{< relref "/ChapterFour/1600~1699/1652.Defuse-the-Bomb.md" >}})|Easy||||61.0%|
172+
|1652|Defuse the Bomb|[Go]({{< relref "/ChapterFour/1600~1699/1652.Defuse-the-Bomb.md" >}})|Easy||||60.9%|
173173
|1656|Design an Ordered Stream|[Go]({{< relref "/ChapterFour/1600~1699/1656.Design-an-Ordered-Stream.md" >}})|Easy||||82.2%|
174174
|1672|Richest Customer Wealth|[Go]({{< relref "/ChapterFour/1600~1699/1672.Richest-Customer-Wealth.md" >}})|Easy||||88.1%|
175175
|1700|Number of Students Unable to Eat Lunch|[Go]({{< relref "/ChapterFour/1700~1799/1700.Number-of-Students-Unable-to-Eat-Lunch.md" >}})|Easy||||67.6%|

website/content/ChapterTwo/Bit_Manipulation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ X & ~X = 0
6464
|0397|Integer Replacement|[Go]({{< relref "/ChapterFour/0300~0399/0397.Integer-Replacement.md" >}})|Medium| O(n)| O(1)||33.6%|
6565
|0401|Binary Watch|[Go]({{< relref "/ChapterFour/0400~0499/0401.Binary-Watch.md" >}})|Easy| O(1)| O(1)||48.8%|
6666
|0405|Convert a Number to Hexadecimal|[Go]({{< relref "/ChapterFour/0400~0499/0405.Convert-a-Number-to-Hexadecimal.md" >}})|Easy| O(n)| O(1)||44.7%|
67-
|0421|Maximum XOR of Two Numbers in an Array|[Go]({{< relref "/ChapterFour/0400~0499/0421.Maximum-XOR-of-Two-Numbers-in-an-Array.md" >}})|Medium| O(n)| O(1)|❤️|54.6%|
67+
|0421|Maximum XOR of Two Numbers in an Array|[Go]({{< relref "/ChapterFour/0400~0499/0421.Maximum-XOR-of-Two-Numbers-in-an-Array.md" >}})|Medium| O(n)| O(1)|❤️|54.5%|
6868
|0461|Hamming Distance|[Go]({{< relref "/ChapterFour/0400~0499/0461.Hamming-Distance.md" >}})|Easy| O(n)| O(1)||73.3%|
6969
|0476|Number Complement|[Go]({{< relref "/ChapterFour/0400~0499/0476.Number-Complement.md" >}})|Easy| O(n)| O(1)||65.2%|
7070
|0477|Total Hamming Distance|[Go]({{< relref "/ChapterFour/0400~0499/0477.Total-Hamming-Distance.md" >}})|Medium| O(n)| O(1)||50.8%|

website/content/ChapterTwo/Math.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ weight: 12
5858
|0753|Cracking the Safe|[Go]({{< relref "/ChapterFour/0700~0799/0753.Cracking-the-Safe.md" >}})|Hard||||52.8%|
5959
|0775|Global and Local Inversions|[Go]({{< relref "/ChapterFour/0700~0799/0775.Global-and-Local-Inversions.md" >}})|Medium||||46.2%|
6060
|0781|Rabbits in Forest|[Go]({{< relref "/ChapterFour/0700~0799/0781.Rabbits-in-Forest.md" >}})|Medium||||56.0%|
61+
|0810|Chalkboard XOR Game|[Go]({{< relref "/ChapterFour/0800~0899/0810.Chalkboard-XOR-Game.md" >}})|Hard||||50.7%|
6162
|0812|Largest Triangle Area|[Go]({{< relref "/ChapterFour/0800~0899/0812.Largest-Triangle-Area.md" >}})|Easy||||59.2%|
6263
|0836|Rectangle Overlap|[Go]({{< relref "/ChapterFour/0800~0899/0836.Rectangle-Overlap.md" >}})|Easy||||43.1%|
6364
|0869|Reordered Power of 2|[Go]({{< relref "/ChapterFour/0800~0899/0869.Reordered-Power-of-2.md" >}})|Medium||||61.2%|
@@ -82,7 +83,7 @@ weight: 12
8283
|1037|Valid Boomerang|[Go]({{< relref "/ChapterFour/1000~1099/1037.Valid-Boomerang.md" >}})|Easy||||37.5%|
8384
|1073|Adding Two Negabinary Numbers|[Go]({{< relref "/ChapterFour/1000~1099/1073.Adding-Two-Negabinary-Numbers.md" >}})|Medium||||34.8%|
8485
|1093|Statistics from a Large Sample|[Go]({{< relref "/ChapterFour/1000~1099/1093.Statistics-from-a-Large-Sample.md" >}})|Medium||||48.3%|
85-
|1154|Day of the Year|[Go]({{< relref "/ChapterFour/1100~1199/1154.Day-of-the-Year.md" >}})|Easy||||49.4%|
86+
|1154|Day of the Year|[Go]({{< relref "/ChapterFour/1100~1199/1154.Day-of-the-Year.md" >}})|Easy||||49.3%|
8687
|1175|Prime Arrangements|[Go]({{< relref "/ChapterFour/1100~1199/1175.Prime-Arrangements.md" >}})|Easy||||51.8%|
8788
|1201|Ugly Number III|[Go]({{< relref "/ChapterFour/1200~1299/1201.Ugly-Number-III.md" >}})|Medium||||26.5%|
8889
|1217|Minimum Cost to Move Chips to The Same Position|[Go]({{< relref "/ChapterFour/1200~1299/1217.Minimum-Cost-to-Move-Chips-to-The-Same-Position.md" >}})|Easy||||70.8%|
@@ -95,7 +96,7 @@ weight: 12
9596
|1641|Count Sorted Vowel Strings|[Go]({{< relref "/ChapterFour/1600~1699/1641.Count-Sorted-Vowel-Strings.md" >}})|Medium||||75.2%|
9697
|1648|Sell Diminishing-Valued Colored Balls|[Go]({{< relref "/ChapterFour/1600~1699/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}})|Medium||||30.8%|
9798
|1680|Concatenation of Consecutive Binary Numbers|[Go]({{< relref "/ChapterFour/1600~1699/1680.Concatenation-of-Consecutive-Binary-Numbers.md" >}})|Medium||||52.3%|
98-
|1685|Sum of Absolute Differences in a Sorted Array|[Go]({{< relref "/ChapterFour/1600~1699/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array.md" >}})|Medium||||63.3%|
99+
|1685|Sum of Absolute Differences in a Sorted Array|[Go]({{< relref "/ChapterFour/1600~1699/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array.md" >}})|Medium||||63.4%|
99100
|1716|Calculate Money in Leetcode Bank|[Go]({{< relref "/ChapterFour/1700~1799/1716.Calculate-Money-in-Leetcode-Bank.md" >}})|Easy||||64.3%|
100101
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|
101102

0 commit comments

Comments
 (0)