题目:

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.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.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.Hint:If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?

分析:该题重在找规律,不在于编码,找不到真正的规律,代码会报Time Limit Exceeded. 下面AC代码:

    public boolean canWinNim(int n) {return n%4 != 0;}

附上Time Limit Exceeded代码,当n=1348820612时,代码不能AC:

    public boolean canWinNim(int n) {if (n <= 3) {return true;} else if (n == 4) {return false;} else {boolean a1 = false, a2 = false, a3 = true;for (int i = 5; i <= n; i++) {boolean bn = !(a1 && a2 && a3);a1 = a2;a2 = a3;a3 = bn;    }return a3;}}

Time Limit Exceeded

转载于:https://www.cnblogs.com/lasclocker/p/4927290.html

[leetcode] Nim Game相关推荐

  1. LeetCode Nim游戏 题解

    题述: 你和你的朋友,两个人一起玩 Nim 游戏: 桌子上有一堆石头. 你们轮流进行自己的回合, 你作为先手 . 每一回合,轮到的人拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者. 假设你 ...

  2. Leetcode Nim 游戏

    你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断 ...

  3. LeetCode Nim Game

    题意:给出n个石头,每个人只能取1-3个石头,两个人轮流取,最后一个取尽的算赢,你最先开始取 思路:因为每次只能取1-3个,如果石头的个数为4的整数倍,你是不可能赢的,如果不是4的整数倍,你首先取n% ...

  4. LeetCode实战:Nim 游戏

    背景 为什么你要加入一个技术团队? 如何加入 LSGO 软件技术团队? 我是如何组织"算法刻意练习活动"的? 为什么要求团队的学生们写技术Blog 题目英文 You are pla ...

  5. LeetCode 292 Nim Game

    LeetCode 292 Nim Game https://leetcode.com/problems/nim-game/ 当能被4整除时,才会输. bool canWinNim(int n) {re ...

  6. LeetCode题目笔记——292. Nim 游戏 (脑筋急转弯C++/Python)

    文章目录 题目描述 题目难度--简单 方法一:脑筋急转弯 代码/C++/Python 总结 题目描述 你和你的朋友,两个人一起玩 Nim 游戏: 桌子上有一堆石头. 你们轮流进行自己的回合, 你作为先 ...

  7. leetcode刷题:292. Nim 游戏(Java)

    题目描述 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函 ...

  8. LeetCode:292. Nim Game Nim 游戏(C语言)

    题目描述: 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个 ...

  9. 【LeetCode击败99%+】Nim 游戏

    题目 你和你的朋友,两个人一起玩 Nim 游戏: 桌子上有一堆石头. 你们轮流进行自己的回合,你作为先手. 每一回合,轮到的人拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者. 假设你们每一 ...

  10. LeetCode 292. Nim Game

    292. Nim Game 尼姆游戏 You are playing the following Nim Game with your friend: 您正在和您的朋友玩以下NIM游戏: There ...

最新文章

  1. 用Memcache守护程序把数据缓存到内存二
  2. 【转贴】Decoda Tutorial LUA调式器
  3. 121_Power Query之R.Execute的read.xlsxODBC
  4. BMVC18|无监督深度关联学习大幅提高行人重识别性能(附Github地址)
  5. Linux系统与网络服务管理技术
  6. 微信小程序富文本编辑器 editor 组件源码
  7. 联想y430完全拆机图解_视频深度拆解:联想IdeaPad Y430全揭秘
  8. 反向题在测试问卷信效度_调查问卷的信效度分析
  9. 首款国产开源数据库TBase核心架构演进
  10. TCP/IP基础(三)
  11. html页面宽度1920,网页banner尺寸1920
  12. U盘文件如何恢复?u盘还原,必学的4招(2023已更新)
  13. 有趣现象:同一个java文件中有2个类,一个public,一个无类修饰符,各有一个main函数,谁在前先执行谁!
  14. SingleTask和SingleInstance详解
  15. 强化学习入门——以Q-Learning为实例
  16. Jetson Nano 人工智能计算
  17. git的GUI工具Sourcetree使用及命令行对比
  18. 吉利闯入“工业互联网”,李书福的“微笑曲线”能否如愿?
  19. 产品经理获得最快成长速度的方法
  20. Oracle CBO RBO

热门文章

  1. Android 六大布局之 LinearLayout( 线性布局)
  2. 阶段3 1.Mybatis_09.Mybatis的多表操作_9 mybatis多对多操作-查询用户获取用户所包含的角色信息...
  3. kubernetes ingress-nginx原理
  4. BugBugBugBugBugBugBugBugBugBugBugBugBugBugBug
  5. eclipse安装activiti 工作流插件
  6. 我的大学生活-4-21-吕家尧
  7. Swift - 继承UIView实现自定义可视化组件(附记分牌样例)
  8. 如何将位图格式图片文件(.bmp)生成geotiff格式图片?
  9. CentOS 关闭暂不需要的系统服务
  10. Delphi 重启应用程序