leetcode 810. Chalkboard XOR Game


原题地址:https://leetcode.com/problems/chalkboard-xor-game/

题目

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.

Note:

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

    这道题有些歧义,几位大佬在讨论为啥[1,2,3]的结果是true,他们认为最开始是一定由Alice开始的,可是要想解这道题,如果最开始结果就是0的话,那么就直接默认Alice赢,所以[1,2,3]的结果为true。
    判断初始条件,只要初始结果不为零,那么一定存在两个不相同的nums[i],这时候只要去选与结果不同的即可。多以只要给定数组为偶数个,那么Alice就可以赢,奇数的话Alice擦掉一个数后就相当于Bob站在了偶数个时候Alice的角度,所以Bob赢。如果只考虑数组长度,就会造成[1,2,3]返回false的情况。

python代码

class Solution:def xorGame(self, nums):""":type nums: List[int]:rtype: bool"""result = 0for i in nums: result = result ^ iif result == 0 or len(nums) % 2 == 0:return Trueelse:return False

版权声明:转载注明 http://blog.csdn.net/birdreamer/article/details/79781069

leetcode 810. Chalkboard XOR Game相关推荐

  1. [Leetcode] 810. Chalkboard XOR Game 解题报告

    题目: We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob tak ...

  2. LeetCode 810 Chalkboard XOR Game【思维】

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

  3. LeetCode 421. Maximum XOR of Two Numbers in an Array--Python解法

    LeetCode 421. Maximum XOR of Two Numbers in an Array–C++,Python解法 LeetCode题解专栏:LeetCode题解 我做的所有的Leet ...

  4. [Swift]LeetCode810. 黑板异或游戏 | Chalkboard XOR Game

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  5. leetcode 421. Maximum XOR of Two Numbers in an Array | 421. 数组中两个数的最大异或值(位运算,Trie前缀树)

    题目 https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/ 题解 自己没有思路,看了答案之后自己写的. 参考:Py ...

  6. LeetCode 810. 黑板异或游戏(博弈推理)

    1. 题目 一个黑板上写着一个非负整数数组 nums[i] . 小红和小明轮流从黑板上擦掉一个数字,小红先手. 如果擦除一个数字后,剩余的所有数字按位异或运算得出的结果等于 0 的话,当前玩家游戏失败 ...

  7. LintCode——Chalkboard XOR Game(黑板游戏)

    黑板游戏: We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob ta ...

  8. leetcode 421. Maximum XOR of Two Numbers in an Array

    Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  9. leetcode 810. 黑板异或游戏

    黑板上写着一个非负整数数组 nums[i] .Alice 和 Bob 轮流从黑板上擦掉一个数字,Alice 先手.如果擦除一个数字后,剩余的所有数字按位异或运算得出的结果等于 0 的话,当前玩家游戏失 ...

最新文章

  1. 网页制作插入新的元素,并且为插入的元素添加事件
  2. 学python有哪些书推荐-有哪些Python学习路线值得推荐?线路及书籍推荐都在这里...
  3. SPOJ - PHRASES Relevant Phrases of Annihilation —— 后缀数组 出现于所有字符串中两次且不重叠的最长公共子串...
  4. Java Socket 教程
  5. go Windows Service
  6. select获取下拉框的值 下拉框默认选中
  7. 在VC++ 6.0下利用共享内存、消息实现内部进程通讯
  8. 【MFC开发(5)】单选按钮控件RADIOBUTTON
  9. 恒生PB与讯投PB区别
  10. 腾讯X5WebView集成使用
  11. 利用DynamipsGUI制作BSCI课程实验拓扑
  12. 语音信号处理1 ----- 基础知识
  13. 《OpenGL编程指南(原书第9版)》——2.8 SPIR-V
  14. 【已解决】C盘-系统引导盘扩大容量
  15. opencv-python 中直线拟合函数cv.fitLine()的2D直线拟合(适合小白观看)
  16. html换行不出现横线,wps为什么在换行空格下划线不显示
  17. APP功能测试点(全)
  18. 区间DP--LeetCode5498石子游戏
  19. 2020年下半年系统集成项目管理资料、精心整理
  20. 电脑d盘和e盘不见了怎么恢复?

热门文章

  1. 唯品会(VIP)按关键词搜索 API
  2. AndroidManifest--详细理解
  3. 【毕业设计】8-基于STM32的水质_浊度检测仪设计与实现(原理图+源码+实物照片+答辩论文)
  4. overload override
  5. win10菜单栏等底部状态栏打不开解决办法
  6. Matlab画图技巧
  7. 特殊的搜狗拼音输入法
  8. 清华大学 博士后 原来入的计算机科学与技术 现在能入软件工程吗,清华大学软件学院...
  9. 双能CT的基本原理及理解笔记
  10. ansys经典界面分析工字钢_ansys工字钢实例分析.doc