Skip to content

Commit 3e07240

Browse files
committed
solution 292-Nim-Game
1 parent b367b66 commit 3e07240

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

Algorithms/C/292-Nim-Game.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# 292. Nim Game (C Language)
2+
3+
## Problem
4+
5+
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
6+
7+
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
8+
9+
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
10+
11+
Credits:
12+
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
13+
14+
15+
16+
17+
##### Solution 1.
18+
19+
這題是在兩個玩家情況下輪流拿石頭,拿到最後一個獲勝,題目有說到剩 4 顆石頭必輸,所以要盡可能想辦法讓對方拿到 4 顆(4的倍數),以下舉幾個所有可能情況。
20+
- 1~3顆全拿(獲勝)
21+
- 4顆(輸)
22+
- 我方拿1、2、3顆對方都獲勝
23+
- 5顆(獲勝)
24+
- 我方拿1顆對方剩4顆情況
25+
- 6顆(獲勝)
26+
- 我方拿2顆對方剩4顆情況
27+
- 7顆(獲勝)
28+
- 我方拿3顆對方剩4顆情況
29+
- 8顆(輸)
30+
- 我方拿1、2、3顆對方都獲勝
31+
.
32+
.
33+
.
34+
重複
35+
36+
- 數學解法
37+
- Run Time: 0 ms
38+
- 時間複雜度: O(1)
39+
- 空間複雜度: O(1)
40+
41+
```c
42+
bool canWinNim(int n) {
43+
if(n<4)
44+
return 1;
45+
else
46+
return (n%4)!=0;
47+
}
48+
```

Algorithms/Java/292-Nim-Game.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 292. Nim Game (Java)
2+
3+
## Problem
4+
5+
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
6+
7+
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
8+
9+
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
10+
11+
Credits:
12+
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
13+
14+
15+
16+
17+
##### Solution 1.
18+
19+
這題是在兩個玩家情況下輪流拿石頭,拿到最後一個獲勝,題目有說到剩 4 顆石頭必輸,所以要盡可能想辦法讓對方拿到 4 顆(4的倍數),以下舉幾個所有可能情況。
20+
- 1~3顆全拿(獲勝)
21+
- 4顆(輸)
22+
- 我方拿1、2、3顆對方都獲勝
23+
- 5顆(獲勝)
24+
- 我方拿1顆對方剩4顆情況
25+
- 6顆(獲勝)
26+
- 我方拿2顆對方剩4顆情況
27+
- 7顆(獲勝)
28+
- 我方拿3顆對方剩4顆情況
29+
- 8顆(輸)
30+
- 我方拿1、2、3顆對方都獲勝
31+
.
32+
.
33+
.
34+
重複
35+
36+
- 數學解法
37+
- Run Time: 0 ms
38+
- 時間複雜度: O(1)
39+
- 空間複雜度: O(1)
40+
41+
```java
42+
class Solution {
43+
public boolean canWinNim(int n) {
44+
if(n<4)
45+
return true;
46+
else
47+
return (n%4)!=0;
48+
}
49+
}
50+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
53.|[Maximum Subarray](https://leetcode.com/problems/maximum-subarray/description/)|Array|Easy|[C](/Algorithms/C/53-Maximum-Subarray.md) [Java](/Algorithms/Java/53-Maximum-Subarray.md)|
1616
69.|[Sqrt(x)](https://leetcode.com/problems/sqrtx/description/)|Math|Easy|[C](/Algorithms/C/69-Sqrt(x).md) [Java](/Algorithms/Java/69-Sqrt(x).md)|
1717
242.|[Valid Anagram](https://leetcode.com/problems/valid-anagram/description/)|String|Easy|[C](/Algorithms/C/242-Valid-Anagram.md) [Java](/Algorithms/Java/242-Valid-Anagram.md)|
18+
292.|[Nim Game](https://leetcode.com/problems/nim-game/description/)|Math|Easy|[C](/Algorithms/C/292-Nim-Game.md) [Java](/Algorithms/Java/292-Nim-Game.md)|
1819
344.|[Reverse String](https://leetcode.com/problems/reverse-string/description/)|String |Easy|[C](/Algorithms/C/344-Reverse-String.md) [Java](/Algorithms/Java/344-Reverse-String.md)
1920

2021

0 commit comments

Comments
 (0)