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 c93f25e

Browse files
committed
feat: add solutions to lc problem: No.0292.Nim Game
1 parent cdfac37 commit c93f25e

File tree

6 files changed

+75
-4
lines changed

6 files changed

+75
-4
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,61 @@
5050
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
5151
</ul>
5252

53-
5453
## 解法
5554

5655
<!-- 这里可写通用的实现逻辑 -->
5756

57+
第一个得到 4 的倍数(即 n % 4 == 0)的将会输掉比赛。
58+
59+
证明:
60+
61+
1.`n == 4`,无论第一个玩家选择 1/2/3 哪个数字,第二个玩家总能选择剩下的数字,**第一个玩家将会输掉比赛**
62+
1.`4 < n < 8`,即 (n = 5,6,7),第一个玩家可以相应地将数字减少为 4,那么 4 这个死亡数字给到了第二个玩家,第二个玩家将会输掉比赛。
63+
1.`n == 8`,无论第一个玩家选择 1/2/3 哪个数字,都会把 `4 < n < 8` 的数字留给第二个,**第一个玩家将会输掉比赛**
64+
1. ...
65+
1. 依次类推,当玩家拿到 n 这个数字,且 n 能被 4 整除,即 `n % 4 == 0`,他将会输掉比赛,否则他将赢得比赛。
66+
5867
<!-- tabs:start -->
5968

6069
### **Python3**
6170

6271
<!-- 这里可写当前语言的特殊实现逻辑 -->
6372

6473
```python
65-
74+
class Solution:
75+
def canWinNim(self, n: int) -> bool:
76+
return n % 4 != 0
6677
```
6778

6879
### **Java**
6980

7081
<!-- 这里可写当前语言的特殊实现逻辑 -->
7182

7283
```java
84+
class Solution {
85+
public boolean canWinNim(int n) {
86+
return n % 4 != 0;
87+
}
88+
}
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
bool canWinNim(int n) {
97+
return n % 4 != 0;
98+
}
99+
};
100+
```
101+
102+
### **Go**
73103
104+
```go
105+
func canWinNim(n int) bool {
106+
return n%4 != 0
107+
}
74108
```
75109

76110
### **...**

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,38 @@ In all outcomes, your friend wins.
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def canWinNim(self, n: int) -> bool:
62+
return n % 4 != 0
6163
```
6264

6365
### **Java**
6466

6567
```java
68+
class Solution {
69+
public boolean canWinNim(int n) {
70+
return n % 4 != 0;
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
bool canWinNim(int n) {
81+
return n % 4 != 0;
82+
}
83+
};
84+
```
85+
86+
### **Go**
6687
88+
```go
89+
func canWinNim(n int) bool {
90+
return n%4 != 0
91+
}
6792
```
6893

6994
### **...**
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
bool canWinNim(int n) {
4+
return n % 4 != 0;
5+
}
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func canWinNim(n int) bool {
2+
return n%4 != 0
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Solution {
22
public boolean canWinNim(int n) {
3-
return (n & 3) != 0;// n%4 != 0
3+
return n % 4 != 0;
44
}
55
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def canWinNim(self, n: int) -> bool:
3+
return n % 4 != 0

0 commit comments

Comments
(0)

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