Question

292. Nim Game

Solution

思路:试着列举一下,就能发现一个n只要不是4的倍数,就能赢。

n 是否能赢
1 true
2 true
3 true
4 false 不论删除几,对方都能一把赢
5 true 删除1,还剩4,对方先手,对方输
6 true 删除2,还剩4,对方先手,对方输
7 true 删除3,还剩4,对方先手,对方输
8 false 不论删除几,都能被对方造成还剩4,已方先手,我们就输
9 true
10 true

Java实现:

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

292. Nim Game - LeetCode的更多相关文章

  1. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

  2. LN : leetcode 292 Nim Game

    lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...

  3. 【Leetcode】292. Nim Game

    problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...

  4. lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II

    变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...

  5. Java实现 LeetCode 292 Nim游戏

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

  6. 292. Nim Game

    292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on ...

  7. 292. Nim Game(C++)

    292. Nim Game(C++) You are playing the following Nim Game with your friend: There is a heap of stone ...

  8. 292. Nim游戏

    292. Nim游戏 class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: b ...

  9. LeetCode 292. Nim Game (取物游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

随机推荐

  1. 记录axios高效率并发的方法

    //  首先我的请求是统一管理 方便创建拦截器 export function login(parameter) {   return axios({     url: api.Login,      ...

  2. 设计模式-观察者模式Observe的实现

    using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> / ...

  3. add jars、add external jars、add library、add class folder的区别

    add external jars = 增加工程外部的包add jars = 增加工程内包add library = 增加一个库add class folder = 增加一个类文件夹add jar是表 ...

  4. fetch和axios区别,摘自Stack Overflow网站答案

    fetch 请求let url = 'https://someurl.com'; let options = { method: 'POST', mode: 'cors', headers: { 'A ...

  5. Virtual Function(虚函数)in c++

    Virtual Function(虚函数)in c++ 用法: virtual void log() { std::cout << "hello world!" < ...

  6. Blazor组件自做一 : 使用JS隔离封装viewerjs库

    Viewer.js库是一个实用的js库,用于图片浏览,放大缩小翻转幻灯片播放等实用操作 本文相关参考链接 JavaScript 模块中的 JavaScript 隔离 Viewer.js工程 Blazo ...

  7. Python入门-字符串格式化

    一.不推荐使用:%号 #正常按照位置传递参数 print('%s asked %s to do something' % ('egon', 'lili')) #先后顺序不能乱 #字典传递参数 prin ...

  8. APSI - 2

    上一篇 APSI-1 其实就是对开源库README文件的一个翻译加上自己的一点点理解,因为篇幅过大,导致继续编辑有些卡顿,所以新开一篇继续. 前面介绍了APSI的大致技术.优化方法.以及举例说明了主要 ...

  9. RMI反序列化学习

    RMI学习 1.RMI简介 RMI(Remote Method Invocation),远程方法调用方法,其实就是本地java虚拟机要调用其他java虚拟机的方法,两个虚拟机可以是运行在相同计算机上的 ...

  10. Water 2.5.9 发布,一站式服务治理平台

    Water(水孕育万物...) Water 为项目开发.服务治理,提供一站式解决方案(可以理解为微服务架构支持套件).基于 Solon 框架开发,并支持完整的 Solon Cloud 规范:已在生产环 ...