Java实现 LeetCode 810 黑板异或游戏 (分析)
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 黑板异或游戏 (分析)的更多相关文章
- [Swift]LeetCode810. 黑板异或游戏 | Chalkboard XOR Game
		We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob take tu ... 
- 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 ... 
- 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. ... 
- 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 ... 
- 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 ... 
- 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 ... 
- 51nod-1661 1661 黑板上的游戏(组合游戏)
		题目链接: 1661 黑板上的游戏 Alice和Bob在黑板上玩一个游戏,黑板上写了n个正整数a1, a2, ..., an,游戏的规则是这样的:1. Alice占有先手主动权.2. 每个人可以选取一 ... 
- 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 ... 
- 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 ... 
随机推荐
- 环境篇:Superset
			环境篇:Superset Superset 是什么? Apache Superset 是一个开源.现代.轻量的BI分析工具,能够对接多种数据源,拥有丰富的图表展示形式.支持自定义仪表盘,用户界面友好, ... 
- Spark离线日志分析,连接Spark出现报错
			首先,我的代码是这样的 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} object ... 
- webpack4使用出现ERROR in Template execution failed: ReferenceError: HtmlwebpackPlugin is not defined
			问题描述 博主在使用webpack4的时候,使用了ejs文件,先附上ejs中的代码: <!doctype html> <html lang="zh-CN"> ... 
- jvm入门及理解(五)——运行时数据区(虚拟机栈)和本地方法接口
			一.虚拟机栈背景 由于跨平台性的设计,java的指令都是根据栈来设计的.不同平台CPU架构不同,所以不能设计为基于寄存器的. 优点是跨平台,指令集小,编译器容易实现,缺点是性能下降,实现同样的功能需要 ... 
- python读取excel所有数据(cmd界面)
			python读取excel所有数据(cmd界面) cmd界面显示excel数据 代码 import xlrd import os from prettytable import PrettyTable ... 
- 2018-06-24 js BOM对象
			BOM对象: Browser Object Model 即浏览器对象模型: 包含: window:窗口对象 alert();//警示框 confirm();//确认框 prompt();//输入提示框 ... 
- ScheduleMaster新特性之延时任务初体验
			ScheduleMaster在上个月底更新到了2.0版本,在功能和代码以及文档上都往前跨了很大一步,详细信息可以参考这篇文章:https://www.cnblogs.com/hohoa/p/12772 ... 
- MySQL++:Liunx - MySQL 主从复制
			目标:搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,实现主从复制 环境:虚拟机 主数据库:192.168.211.101 从数据库:192.168.211.102 MySQL 安装可参 ... 
- spark机器学习从0到1介绍入门之(一)
			一.什么是机器学习 机器学习(Machine Learning, ML)是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行 ... 
- 使用phoenix踩的坑与设计思考
			本文主要介绍在压测HBase的二级索引phoenix时踩的一个坑,使用时需要特别注意,而且背后的原因也很有意思,可以看出HBase和Phoenix对元数据设计上的差异. 1.问题介绍 在做phoeni ... 
