题目

We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.)

Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins.

Return True if and only if Alice wins the game, assuming both players play optimally.

Example:
Input: nums = [1, 1, 2]
Output: false
Explanation:
Alice has two choices: erase 1 or erase 2.
If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose.
If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose.

Notes:

  • 1 <= N <= 1000.
  • 0 <= nums[i] <= 2^16.

思路

这么偏数学和技巧的题目,估计FLAG等公司不会考的。但是分析过程却很有趣:

1)如果xor == 0,那就说明Alice已经赢了,因为上一把Bob的行为已经导致了xor == 0。

2)如果xor != 0,并且数组的长度为偶数,那么Alice一定会赢。这是因为:当数组长度为偶数的时候,不可能所有的数都相同(否则xor == 0)。此时Alice总是可以找到两个不同的数,并且erase掉其中一个,这样一定不会引起xor == 0。所以Alice一定会赢。

再看看如果xor != 0 && nums.size() % 2 != 0的时候,会发生什么吧:Alice此时会被迫擦掉一个数,如果擦掉的这个数引起了xor == 0,那么她就立刻输了;否则她擦掉的数虽然不会导致xor == 0,但是此时数组中剩下了偶数个元素,那么Bob就可以采用和Alice相同的策略把Alice干掉,因为此时xo != 0 && nums.size() == 0,满足上面分析中的条件2)。

所以Alice会赢当且仅当xor == 0 || nums.size() % 2 == 0。

代码

class Solution {
public:bool xorGame(vector<int>& nums) {int xo = 0;for (int n: nums) {xo ^= n;}return xo == 0 || nums.size() % 2 == 0;}
};

[Leetcode] 810. Chalkboard XOR Game 解题报告相关推荐

  1. leetcode 810. Chalkboard XOR Game

    leetcode 810. Chalkboard XOR Game 原题地址:https://leetcode.com/problems/chalkboard-xor-game/ 题目 We are ...

  2. LeetCode 810 Chalkboard XOR Game【思维】

    传送门 题意: 给定n个数, 两个人轮流上去删除一个数字, 如果在某个人删除后, 剩下的数字异或等于0,那么这个人就输了, Alice 先手, 如果它能赢return TRUE. 思路: 考思维, 首 ...

  3. LeetCode 167.Two Sum II 解题报告

    LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...

  4. [LeetCode]844. Backspace String Compare 解题报告(C++)

    [LeetCode]844. Backspace String Compare 解题报告(C++) 题目描述 Given two strings S and T, return if they are ...

  5. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  6. Leetcode 第133场周赛解题报告

    今天参加了leetcode的周赛,算法比赛,要求速度比较快.有思路就立马启动,不会纠结是否有更好的方法或代码可读性.只要在算法复杂度数量级内,基本上是怎么实现快速就怎么来了. 比赛时先看的第二题,一看 ...

  7. 【LeetCode】934. Shortest Bridge 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + BFS 相似题目 参考资料 日期 题目地 ...

  8. 【LeetCode】127. Word Ladder 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...

  9. 【LeetCode】723. Candy Crush 解题报告 (C++)

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

最新文章

  1. 【工具篇】抓包中的王牌工具—Fiddler (1-环境搭建)
  2. 用python实现复选框树_如何使用Python中的复选框创建树视图
  3. 【2020年第12届全国大学生数学竞赛——资源分享 】【1~11届省赛决赛考题及题解(数学类、非数学类)、推荐学习网址、复习备考书籍推荐】
  4. 子慕谈设计模式系列(二)——设计模式六大原则
  5. 如何解决php 生成验证码图片不显示问题
  6. heroku创建linux主机,将Yesod部署到Heroku,无法静态构建
  7. 请求过程中,需要证书认证,这种情况下如何处理
  8. oracle修改实例监听端口,oracle之 单实例监听修改端口
  9. node中exports和module.exports的关系及使用
  10. ANSYS入门例程笔记
  11. excel2019批量删除空白行的方法
  12. 为什么需要Code Review?
  13. 优酷播放黑科技 | 基于WebRTC实现的直播“云多视角“技术解析
  14. python平安夜代码加文案
  15. 爱因斯坦广义相对论:引力是时空的曲率
  16. linux服务器强制关机,Mac强制关机的4种方法以备不时之需
  17. Axure实战——改变元件尺寸、增减商品数量、图片边框移动
  18. 期货与现货结合(期货与现货结合的例子)
  19. 秘制牛肉团队博客目录
  20. MySql基础学习笔记(一)

热门文章

  1. TI RTOS BLE CC2642 看门狗 Watcdog
  2. onsemi安森美FDMS86252L 50V 12A 56mΩ N沟道屏蔽门极MOSFET管
  3. 笔试一道honor的嵌入式软件程序编写题目【c语言字符串】
  4. matlab误码率计算函数,matlab通信系统性能估计(误码率、误比特率、眼图、星座图….) | 学步园...
  5. 转:杰克·韦尔奇:我的生命应该如何度过?
  6. 江苏省数学建模省赛答辩准备
  7. 经典例题C语言程序解决数学问题
  8. python数据分析师 前景_数据分析师的前景怎么样? 本人是程序员,最近对数据分析有兴趣……...
  9. VISUAL SVN安装 及客户端使用
  10. Android在线购物商城 app端+后台