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. 对H.264帧类型判断方法

    背景描述 我们经常在网络直播推流或者客户端拉流的时候,需要对获取到的H.264视频帧进行判断后处理,我们经常获取到各种不同的视频数据0x67 0x68 0x65 0x61,0x27 0x28 0x25 ...

  2. 用EasyClient开源项目采集Windows摄像头/麦克风的音视频进行RTSP直播

    EasyClient是EasyDarwin开源流媒体团队开发的一款功能丰富的开源PC客户端项目,目前支持Windows.Android版本,后续将支持ios版本,其中Windows版本的EasyCli ...

  3. UI标签库专题四:JEECG智能开发平台 Upload(上传标签)

     1. Upload(上传标签) 1.1.  參数 属性名 类型 描写叙述 是否必须 默认值 id string 上传控件唯一标示 是 null name string 控件name 是 null ...

  4. Promise 源码分析

    前言 then/promise项目是基于Promises/A+标准实现的Promise库,从这个项目当中,我们来看Promise的原理是什么,它是如何做到的,从而更加熟悉Promise 分析 从ind ...

  5. Vue一次性简洁明了引入所有公共组件

    使用场景 -在开发过程中,往往会有很多自己定义公用组件,我们通过import 导入,components挂载到实例上就行,项目刚开始还好,但是随着项目迭代,组件越来越多,同一个组件不同的方式用需要重复 ...

  6. Windows程序设计(1)——Win32运行原理(二)

    创建进程 1 进程和线程 2 应用程序的启动过程 3 CreateProcess函数 4 实例 3 创建进程 3.1 进程和线程 进程通常被定义为一个存在运行的程序的实例.进程是一个正在运行的程序,它 ...

  7. Django_model进阶

    Django-model进阶   QuerySet 可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. >>> Ent ...

  8. 简单使用FusionCharts(Free)

    介绍 FusionCharts Free 是一个跨平台,跨浏览器的flash图表组件解决方案,能够被 ASP.NET, ASP, PHP, JSP, ColdFusion, Ruby on Rails ...

  9. JSTL取整、读取数组、字符串连接

    以通过formatNumber去掉小数. <fmt:formatNumber type='number' value='${(tv.timeLong-tv.timeLong%60)/60 }' ...

  10. 【转载】如何在 C#中访问 JavaScript函数?

    如何在 C#中访问 JavaScript函数? 时间:13-10-17 栏目:Unity3D教程 作者:zqcyou 评论:0     如何在 C#中访问 JavaScript函数?答案如下:c#代码 ...