problem

292. Nim Game

solution

class Solution {
public:
bool canWinNim(int n) {
return n%;
}
};

来generalize一下这道题,当可以拿1~n个石子时,那么个数为(n+1)的整数倍时一定会输,我们试着证明一下这个结论,若当前共有m*(n+1)个石子,那么:

当m=1时,即剩n+1个的时候,肯定会输,因为不管你取1~n中的任何一个数字,另一个人都可以取完。
当m>1时,即有m*(n+1)的时候,不管你先取1~n中的任何一个数字x,另外一个人一定会取n+1-x个,这样总数就变成了(m-1)*(n+1),第二个人就一直按这个策略取,那么直到剩n+1个的时候,就便变成m=1的情况,一定会输。

参考

1. Leetcode_292_Nim Game;

2. GrandYang;

3. Nim Game;

【Leetcode】292. Nim Game的更多相关文章

  1. 【LeetCode】292. Nim Game 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【Leetcode】292. Nim游戏

    题目链接:https://leetcode-cn.com/problems/nim-game/description/ 您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1  ...

  3. 【一天一道LeetCode】#292. Nim Game

    一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. 在Vue中关闭Eslint 的方法

    在vue项目中关闭ESLint方法:找到 webpack.base.conf.js 将这些代码注释掉, { test: /\.(js|vue)$/, loader: 'eslint-loader', ...

  2. linux nat style

      1● nat style 2● link style    

  3. pycharm开发工具,使用

    在pycharm中,打的断点,仅在调试模式下,即debug 模式下,才有效 Use Alt + Shift + C to quickly review your recent changes to t ...

  4. HTML代码转换为JavaScript字符串

    我有时在工作中用到字符串拼接基本上来自于此,链接 http://www.css88.com/tool/html2js/

  5. Java中数据类型相互转化

    1 将String 转换成int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String], ...

  6. 通过改变unity中物体的alpha值实现若隐若现的效果

    RawImage logo = mainLogo.transform.FindChild("back/headBack/Logo").GetComponent<RawImag ...

  7. python之路-python字符编码

    编码的发展 py2的字符编码 py3的字符编码 编码的发展: 编码表:编码是信息从一种形式或格式转换为另一种形式的过程也称为计算机编程语言的代码简称编码.编码表记录着我们常见的明文所对应的计算机二进制 ...

  8. Linux下IP的存储位置

    在linux下,配置多个IP的话,通常是eth0... eth0. ..eth0.x等等, 那么如果要配置大量的IP呢,这么配置也是可以的,但是繁琐,虽说这种情况很少. 对于添加大量IP,有一定规定的 ...

  9. 关于执行findbugs,checkstyle,jacoco插件检测代码,GitHook的脚本编写

    Git钩子的作用: (pre-commit ) 在用户执行 git commit -m "xxx" 命令之前,先执行pre-commit文件中的脚本命令 在pre-commit文件 ...

  10. jenkins部署web项目到tomcat(五)

    (1)maven构建web项目 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...