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.

Notes:

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

Approach #1: C++.

class Solution {
public:
bool xorGame(vector<int>& nums) {
int num = 0;
for (int i : nums)
num ^= i;
return num == 0 || nums.size() % 2 == 0;
}
};

  

Approach #2: Java.

class Solution {
public boolean xorGame(int[] nums) {
int xo = 0;
for (int i : nums)
xo ^= i;
return xo == 0 || nums.length % 2 == 0;
}
}

  

Approach #3: Python.

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

  

Analysis:

Math:

Corner Case: If the XOR of all nums is 0, then A wins.

Now we discuss the more general case where the input doesn't from the corner case.

Proposition: Suppose the current chalkboard is S and the len(S) = N, now it's player P's turn. P can

always make a move if XOR(S) != 0 and N is even.

Proof:

  1. Let X = XOR(S), when X != 0, at least one bit of X must be 1. Let bit 'b' of X be the bit ie., X[b] = 1.
  2. Then we can divide the numbers in S into two groups: U and V, where numbers in U have 0 at bit b, and numbers in V have 1 at bit b.
  3. Initially, len(U) could be even or odd, But len(V) must be odd, otherwise we wouldn't have X[b] = 1, So len(U) must be odd too because of the following:
    • len(V) + len(U) = N
    • len(V) is odd
    • N is even
  4. The fact len(U) is odd implies that there must be at least one number (say Y) in S which has Y[b] = 0.
  5. If player P removes the number Y from S, the result chalkboard S' will have X' = XOR(S') = X xor Y, where X'[b] = 1. So S' != 0.

The explanation come from https://leetcode.com/problems/chalkboard-xor-game/discuss/165396/Detailed-math-explanation-Easy-to-understand

Weekly Contest 78-------->810. Chalkboard XOR Game的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  3. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  4. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

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

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

  6. [LeetCode] Chalkboard XOR Game 黑板亦或游戏

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

  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之Weekly Contest 91

    第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10  ...

  9. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

随机推荐

  1. 关于mongorc.js文件详解

    最近阅读了<<mongodb权威指南第二版>>,发现这本书比之前的第一版好,很多地方讲解很详细.下面就翻译下谈下这个文件. 首先,启动shell的时候,mongorc.js文件 ...

  2. 从S3中导入数据到Dynamodb

    本节如果你已经从Dynamodb中导出过数据,而且导出的文件以及被存入S3.文件内部结构会在Verify Data Export File 中描写叙述. 我们称之前导出数据的原始表为source ta ...

  3. ArrayList概述

    一. ArrayList概述: ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境 ...

  4. DirectShow音频采集pcm,实时编码AAC,附源码

    定期送福利,今天给大家送上Windows中利用DirectShow采集microphone音频,并将采集到的pcm数据,利用FAAC库编码成AAC,进行本地存储或者网络传输. 直接贴代码,解析看注释: ...

  5. 九度OJ 1134:密码翻译 (翻译)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1988 解决:810 题目描述: 在情报传递过程中,为了防止情报被截获,往往需要对情报用一定的方式加密,简单的加密算法虽然不足以完全避免情报 ...

  6. Linux环境下安装ActiveMq

    一.准备安装的tar包 1.将安装包放在服务器上:apache-activemq-5.10.2.tar.gz 2.将安装包解压:tar -zxvf apache-activemq-5.10.2.tar ...

  7. (转)SDP协议概述

    1 简介 SDP 完全是一种会话描述格式, 它不属于传输协议. 它使用不同的适当的传输协议,包括会话通知协议(SAP).会话初始协议(SIP). 实时流协议(RTSP).MIME 扩展协议的电子邮件以 ...

  8. Nodejs通过Thrift操作hbase卡住原因分析及与javascript的垃圾回收机制的关系

    在最近使用Nodejs通过Thrift操作hbase的时候写了个脚本,不断发送http请求,从而取得hbase下所需的数据,但是在run的过程中for循环并没有执行完全,在执行一部分后会卡住,就再也进 ...

  9. android通过DialogFragment实现时间选择

    在android开发中,时间控件是不可或缺的一部分,特别是在设置个人生日或按时间进行搜索时都要用到.Android有内置的DatePicker和timePicker,使用起来也是相当的方便,既可以在布 ...

  10. 在oc中一些常用的宏定义总结

    1.打印CGRect,Size,Point #define NSLogRect(rect) NSLog(@"%s x:%.4f, y:%.4f, w:%.4f, h:%.4f", ...