810. 黑板异或游戏

一个黑板上写着一个非负整数数组 nums[i] 。小红和小明轮流从黑板上擦掉一个数字,小红先手。如果擦除一个数字后,剩余的所有数字按位异或运算得出的结果等于 0 的话,当前玩家游戏失败。 (另外,如果只剩一个数字,按位异或运算得到它本身;如果无数字剩余,按位异或运算结果为 0。)

换种说法就是,轮到某个玩家时,如果当前黑板上所有数字按位异或运算结果等于 0,这个玩家获胜。

假设两个玩家每步都使用最优解,当且仅当小红获胜时返回 true。

示例:

输入: nums = [1, 1, 2]

输出: false

解释:

小红有两个选择: 擦掉数字 1 或 2。

如果擦掉 1, 数组变成 [1, 2]。剩余数字按位异或得到 1 XOR 2 = 3。那么小明可以擦掉任意数字,因为小红会成为擦掉最后一个数字的人,她总是会输。

如果小红擦掉 2,那么数组变成[1, 1]。剩余数字按位异或得到 1 XOR 1 = 0。小红仍然会输掉游戏。

提示:

1 <= N <= 1000

0 <= nums[i] <= 2^16

class Solution {
//小红胜利,必须是异或=0,或者数组长度正好为2的倍数
//数组的长度为2的话,那么一人一个,到先手的时候肯定是没有数字了
public boolean xorGame(int[] nums) {
int x = 0;
for (int v : nums) x ^= v;
return x == 0 || nums.length % 2 == 0;
} }

Java实现 LeetCode 810 黑板异或游戏 (分析)的更多相关文章

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

    We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take tu ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. 51nod-1661 1661 黑板上的游戏(组合游戏)

    题目链接: 1661 黑板上的游戏 Alice和Bob在黑板上玩一个游戏,黑板上写了n个正整数a1, a2, ..., an,游戏的规则是这样的:1. Alice占有先手主动权.2. 每个人可以选取一 ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. pthon-安装新版PyQt5、PyQT5-tool后打不开并Designer.exe提示“This application failed to start because no Qt platform plugin could be initialized.Reinstalling the application the application may fix this program”

    最近学习python,安装网上教程一步一步的安装,网上很多帖子都写的非常详细,不由深深感慨多谢各位不辞辛苦的记录,指导着来自新入门的同学. 但是实际安装中,最理想莫过于一次性安装成功,但自己安装就出现 ...

  2. C#实现局域网聊天 通讯 Socket TCP 多人

    程序分别为服务端与客户端,服务端创建套接字使用多线程侦听多客户端请求 代码需要引用System.Net:和System.Net.Socket:这两个类 分享源码demo:https://pan.bai ...

  3. java-> 分包分层

    项目分层(分包)的作用 程序为什么要分包分层? 以顾客去饭店吃饭案例分析一下: 小饭店: 一个服务员搞定(接待顾客\点菜\炒菜) 大饭店: 迎宾员(是否有预定\ 询问吃中餐还是西餐或者烧烤等\ 几位用 ...

  4. watch 和 计算属性

    作者:纵横链接:https://www.zhihu.com/question/55846720/answer/331760496来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  5. 【雕爷学编程】零基础Python(01)---“投机取巧”的三条途径

    从3月13日报名尝试上网课学习(4天课8.9元),开始接触Python(中文发音“派森”),到今天有一星期了.这两天广泛搜索了一下相关的学习途径,本着“投机取巧”的出发点,居然小有心得,这里一并分享出 ...

  6. DRF路由组件和渲染器组件

    参考博客:https://www.cnblogs.com/wupeiqi/articles/7805382.html

  7. 数据结构----链表Link

    链表简介与数据结构 单向链表也叫单链表,是表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域.这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值. 单向链 ...

  8. Docker镜像下载及加速器设置

    docker远程仓库配置: vim ~/.docker/daemon.json { "registry-mirrors" : [ "https://registry.do ...

  9. nginx操作目录

    nginx配置文件/conf/nginx.conf 错误日志功能:los/error.log 访问日志功能:logs/access.log 站点服务请求功能配置:html/ 禁止访问功能配置 404页 ...

  10. Flex打印功能 (2011-05-21 17:16:14)

    http://blog.sina.com.cn/s/blog_4f925fc30101824k.html