Weekly Contest 78-------->810. Chalkboard XOR Game
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:
- 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.
- 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.
- 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
- The fact len(U) is odd implies that there must be at least one number (say Y) in S which has Y[b] = 0.
- 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的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- [Swift]LeetCode810. 黑板异或游戏 | Chalkboard XOR Game
We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob take tu ...
- [LeetCode] Chalkboard XOR Game 黑板亦或游戏
We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob take tu ...
- LintCode——Chalkboard XOR Game(黑板游戏)
黑板游戏: We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob ta ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
随机推荐
- 通过css选择器class给元素添加cursor的坑
笔者在chrome中遇到了奇特的问题,在通过class给元素添加cursor的自定义图片时.出现了"Invald property value"提示,crosshair.help等 ...
- if __name__
我们经常在python 程序中看到 if __name__ == '__main__' :这代表什么意思? python中 模块是对象,并且所有的模块都有一个内置属性 __name__.一个模 ...
- js怎么select 选中的值
var obj = document.getElementById(”SelectID”); var index = obj.selectedIndex; // 选中索引var text = obj. ...
- vmware虚拟机centos网络配置错误,执行/etc/init.d/network start 或 restart 提示Device eth0 has different MAC address than expected, ignoring
vmware虚拟机centos网络配置错误,执行/etc/init.d/network start 或 restart 提示Device eth0 has different MAC address ...
- 使用Windows API发送HTTP请求
先看一个简单的GET示例 #include <Windows.h> #include <winhttp.h> #include <stdio.h> int main ...
- select监听多个client -- linux函数
使用select函数能够以非堵塞的方式和多个socket通信.程序仅仅是演示select函数的使用,功能很easy,即使某个连接关闭以后也不会改动当前连接数.连接数达到最大值后会终止程序. 1. 程序 ...
- var与变量提升
var是否可以省略 一般情况下,是可以省略var的,但有两点值得注意: 1.var a=1 与 a=1 ,这两条语句一般情况下作用是一样的.但是前者不能用delete删除.不过,绝大多数情况下,这种差 ...
- 代码空间项目 -- 获取当前时间之前的某一天-Calender类的使用
Calendar类的静态方法getInstance()可以初始化一个日历对象:Calendar now = Calendar.getInstance(); 1.Calendar的基本用法calenda ...
- 使用webpack4搭建一个基于Vue的组件库
组内负责的几个项目都有一些一样的公共组件,所以就着手搭建了个公共组件开发脚手架,第一次开发 library,所以是参考着 iview 的配置来搭建的.记录如何使用webpack4搭建一个library ...
- oracle添加表注释和表字段注释
创建Oracle数据库表时加上注释 CREATE TABLE t1( id varchar2(32) primary key,name VARCHAR2(8) NOT NULL, age numbe ...