mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-04 19:28:07 +08:00
【292/877/319 一行代码解决的智力题】【Python3】
【292/877/319 一行代码解决的智力题】【Python3】
This commit is contained in:
@ -149,4 +149,58 @@ int bulbSwitch(int n) {
|
||||
<img src="../pictures/qrcode.jpg" width=200 >
|
||||
</p>
|
||||
|
||||
======其他语言代码======
|
||||
======其他语言代码======
|
||||
|
||||
由[JodyZ0203](https://github.com/JodyZ0203)提供 292. Nim 游戏 Python3 解法代码:
|
||||
|
||||
```Python
|
||||
class Solution:
|
||||
def canWinNim(self, n: int) -> bool:
|
||||
# 如果除于是0,说明是4的倍数,所以必输
|
||||
# 否则不是除于不等于0,说明不是4的倍数,说明必胜
|
||||
return n % 4 != 0
|
||||
```
|
||||
|
||||
由[JodyZ0203](https://github.com/JodyZ0203)提供 877. 石子游戏 Python3 解法代码:
|
||||
|
||||
```Python
|
||||
class Solution:
|
||||
def stoneGame(self, piles: List[int]) -> bool:
|
||||
# 双方都很聪明的前提下, 先手必胜无疑
|
||||
# 先手可以提前观察偶数堆还是基数的石头总数更多
|
||||
return True
|
||||
```
|
||||
|
||||
由[JodyZ0203](https://github.com/JodyZ0203)提供 877. 石子游戏 C++ 解法代码:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
bool stoneGame(vector<int>& piles) {
|
||||
// 双方都很聪明的前提下, 先手必胜无疑
|
||||
return true;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
由[JodyZ0203](https://github.com/JodyZ0203)提供 319. 灯泡开关 Python3 解法代码:
|
||||
|
||||
```Python
|
||||
class Solution:
|
||||
def bulbSwitch(self, n: int) -> int:
|
||||
# 平方根电灯个数之后向下取整即可
|
||||
return floor(sqrt (n))
|
||||
```
|
||||
|
||||
由[JodyZ0203](https://github.com/JodyZ0203)提供 319. 灯泡开关 C++ 解法代码:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
int bulbSwitch(int n) {
|
||||
// 平方根电灯个数之后向下取整即可
|
||||
return floor(sqrt (n));
|
||||
}
|
||||
};
|
||||
```
|
||||
|
Reference in New Issue
Block a user