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 e6075c8

Browse files
committed
commit solution 292
1 parent 50aa41b commit e6075c8

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

‎solution/1-99/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
- [31. 下一个排列](solution/1-99/0031.next-permutation/)
4040
- [32. 最长有效括号](solution/1-99/0032.longest-valid-parentheses/)
4141
- [33. 搜索旋转排序数组](solution/1-99/0033.search-in-rotated-sorted-array/)
42-
- [34. 在排序数组中查找元素的第一个和最后一个位置](solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/)
42+
- [34. 在排序数组中查找元素的第一个和最后一个位置](solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/)
4343
- [35. 搜索插入位置 ✅](solution/1-99/0035.search-insert-position/)
4444
- [36. 有效的数独 ✅](solution/1-99/0036.valid-sudoku/)
4545
- [37. 解数独](solution/1-99/0037.sudoku-solver/)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [292. Nim 游戏](https://leetcode-cn.com/problems/nim-game/description/)
2+
3+
### 题目描述
4+
5+
<p>你和你的朋友,两个人一起玩&nbsp;<a href="https://baike.baidu.com/item/Nim游戏/6737105" target="_blank">Nim 游戏</a>:桌子上有一堆石头,每次你们轮流拿掉&nbsp;1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。</p>
6+
7+
<p>你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。</p>
8+
9+
<p><strong>示例:</strong></p>
10+
11+
<pre><strong>输入:</strong> <code>4</code>
12+
<strong>输出:</strong> false
13+
<strong>解释: </strong>如果堆中有 4 块石头,那么你永远不会赢得比赛;
14+
&nbsp; 因为无论你拿走 1 块、2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走。
15+
</pre>
16+
17+
### 解题思路
18+
19+
20+
21+
### 具体解法
22+
23+
24+
#### **Golang**
25+
```go
26+
func canWinNim(n int) bool {
27+
if n%4 != 0 {
28+
return true
29+
}
30+
return false
31+
}
32+
```
33+
34+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=292 lang=golang
5+
*
6+
* [292] Nim 游戏
7+
*/
8+
9+
// @lc code=start
10+
func canWinNim(n int) bool {
11+
if n%4 != 0 {
12+
return true
13+
}
14+
return false
15+
}
16+
17+
// @lc code=end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCanWinNim(t *testing.T) {
8+
var ret bool
9+
var n int
10+
11+
n = 30
12+
ret = true
13+
if ret != canWinNim(n) {
14+
t.Fatalf("case fails %v\n", ret)
15+
}
16+
17+
n = 40
18+
ret = false
19+
if ret != canWinNim(n) {
20+
t.Fatalf("case fails %v\n", ret)
21+
}
22+
}

‎solution/200-299/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
- [296. 最佳的碰头地点](solution/200-299/0296.best-meeting-point/)
106106
- [297. 二叉树的序列化与反序列化](solution/200-299/0297.serialize-and-deserialize-binary-tree/)
107107
- [298. 二叉树最长连续序列](solution/200-299/0298.binary-tree-longest-consecutive-sequence/)
108-
- [299. 猜数字游戏](solution/200-299/0299.bulls-and-cows/)
108+
- [299. 猜数字游戏](solution/200-299/0299.bulls-and-cows/)
109109

110110

111111

0 commit comments

Comments
(0)

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