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. ReentrantLock源码解析

    ReentrantLock 1 数据结构 从上图可以看出,ReentrantLock的功能都是通过sync这个对象提供的. public class ReentrantLock implements ...

  2. C:单链表的简单实现

    前言 今天整理资料的时候翻出来的文件,发现是以前学习数据结构的时候写的代码,当初是看郝凯老师的视频学习的C语言的数据结构,下面是对于一个单链表的简单的实现. /** ***************** ...

  3. Airtable base

    PC端习惯了SQL Server Express.Access数据库的强大,安卓端再去用Microsoft Office.WPS,能让你怀疑人生.使用Airtable是个不错的方案,workspace ...

  4. [hdu4609]计数方法,FFT

    题目:给一个数组a,从里面任选三个数,求以这三个数为三条边能构成三角形的概率. 思路:由于每个数只能用一次,所以考虑枚举三边中的最大边.先将a数组排序,然后枚举它的每个数x作为最大边,那么问题就是要求 ...

  5. 内容安全策略(CSP)详解

    1.背景 1.1.同源策略 网站的安全模式源于"同源策略",web浏览器允许第一个web页面中的脚本访问页面中的数据,但前提是两个web页面具有相同的源.此策略防止一个页面的恶意脚 ...

  6. 搭建私有镜像仓库registry 2.0

    搭建 docker run -d -p 5000:5000 --restart=always --name registry2 registry:2 就可以将自己的镜像 push到这个私有的镜像仓库 ...

  7. java-> 利用IO操作与递归实现目录的复制

    public class CopyDir { public static void main(String[] args) { copyDir(new File("d:\\a"), ...

  8. docker 修改镜像地址

    一.直接设置 –registry-mirror 参数,仅对当前的命令有效 docker run hello-world --registry-mirror=https://docker.mirrors ...

  9. Azure AD B2C(一)初识

    一,引言(上节回顾) 上一节讲到Azure AD的一些基础概念,以及如何运用 Azure AD 包含API资源,Azure AD 是微软提供的云端的身份标识和资源访问服务,帮助员工/用户/管理员访问一 ...

  10. 读懂这几个关键词,你就能了解 Docker 啦

    基于高度虚拟化所诞生的容器技术,如今已经走向大规模应用.那么容器.虚拟机.Docker.Openstack.Kubernetes 之间又有什么关系,对现在的选择有什么影响呢? 上世纪 60 年代,计算 ...