1

#yyds干货盘点# LeetCode 腾讯精选练习 50 题:Nim 游戏

 1 year ago
source link: https://blog.51cto.com/u_13321676/5884387
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

#yyds干货盘点# LeetCode 腾讯精选练习 50 题:Nim 游戏

精选 原创

灰太狼_cxh 2022-11-24 17:24:56 博主文章分类:leetcode ©著作权

文章标签 最优解 代码实现 文章分类 Java 编程语言 阅读数233

你和你的朋友,两个人一起玩 Nim 游戏:

桌子上有一堆石头。

你们轮流进行自己的回合, 你作为先手 。

每一回合,轮到的人拿掉 1 - 3 块石头。

拿掉最后一块石头的人就是获胜者。

假设你们每一步都是最优解。请编写一个函数,来判断你是否可以在给定石头数量为 n 的情况下赢得游戏。如果可以赢,返回 true;否则,返回 false 。

输入:n = 4

输出:false

解释:以下是可能的结果:

1. 移除1颗石头。你的朋友移走了3块石头,包括最后一块。你的朋友赢了。

2. 移除2个石子。你的朋友移走2块石头,包括最后一块。你的朋友赢了。

3.你移走3颗石子。你的朋友移走了最后一块石头。你的朋友赢了。

在所有结果中,你的朋友是赢家。

输入:n = 1

输出:true

输入:n = 2

输出:true

代码实现:

class Solution {
public boolean canWinNim(int n) {
return n % 4 != 0;
}
}
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK