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. Idea中查看一个类的所有资料及其层级关系

    在Idea中直接Ctrl + t 查看类的子类是可以看到,但是他没有那种层级顺序! 我们可以在类中点击顶部菜单Navigate -----> Type Hierarchy

  2. Azure AD(二)调用受Microsoft 标识平台保护的 ASP.NET Core Web API 上

    一,引言 上一节讲到Azure AD的一些基础概念,以及Azure AD究竟可以用来做什么?本节就接着讲如何在我们的项目中集成Azure AD 包含我们的API资源(其实这里还可以在 SPA单页面应用 ...

  3. Ubuntu 1804 安装xmind8详细过程

    安装比较简单, 折腾了很久,一启动就报错,切换了JDK版本就能用了: 安装 登陆官网,下载xmind8: 下载得到文件xmind-8-update9-linux.zip: 将文件解压至路径xmind下 ...

  4. Go中操作mysql

    Go中操作mysql 首先在mysql里的test数据库中创建数据表 CREATE TABLE `userinfo` ( `uid` INT(10) NOT NULL AUTO_INCREMENT, ...

  5. js理论-函数中的Arguments对象

    详情参考:https://github.com/mqyqingfeng/Blog/issues/14 如果: arguments和实参的关系,以及arguments的属性 附上代码和注解 functi ...

  6. angular前端框架简单小案例

    一.angular表达式 <head> <meta charset="UTF-8"> <title>Title</title> &l ...

  7. css中height, width默认值

    转载自:https://www.cnblogs.com/heyode/p/5973960.html <body> <div class="wrap"> &l ...

  8. docker 学习(一)

    1. docker介绍 1)docker的出现 Docker是诞生于2013年,是dotCloud的一个开源项目,基于Google推出的GO语言实现.遵从Apache2.0协议. 2)docker介绍 ...

  9. fastDFS多线程并发执行出现的问题

    ---------------------  原作者:Java高级开发  来源:CSDN  原文:https://blog.csdn.net/hang1995/article/details/7924 ...

  10. poj1486二分匹配 待填坑

    Sorting Slides Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4777   Accepted: 1867 De ...