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

最新文章

  1. dex文件格式三
  2. 欧洲杯 2016 高清直播 - 观看工具 UEFA-EURO-2016-Play.7z
  3. [python]用Python进行SQLite数据库操作
  4. 实现文本框默认灰色文字,点击消失,如果没输入内容可再返回原来的灰色文字(js版)
  5. OI中神奇的神器fillchar
  6. [转]Android-网络通信框架Volley使用详解
  7. ibatis3.0调用Oracle的存储过程
  8. Delphi工程版本号修改工具
  9. mysql进阶(七)limit的用法
  10. 初学spring boot 一
  11. eclipse、myeclipse写类时,自动生成注释
  12. redis集群之主从架构
  13. [Node.js] 08 - Web Server and REST API
  14. viewport是左下角开始的
  15. ZK分布式锁(未完 待续)
  16. PAT 1138 Postorder Traversal [比较]
  17. css实现文字裁切效果
  18. iScroll示例,下拉刷新,上拉刷新
  19. 使用kibana来进行ElasticSearch的信息查询检索
  20. c#各个版本的特性

热门文章

  1. Hive调优实战
  2. Java 多线程1(转载)
  3. Java多态特性:重载和覆写的比較
  4. Entity Framework(EF)(一)之database first
  5. 2018.11.23-day27 面向对象(大总结)
  6. memcached和一致性hash算法
  7. 发送get和post请求时常用的content-type
  8. it starts (“forks”) a new process for each connection.
  9. atol的实现【转】
  10. 有哪些逾渗模型percolation?