810. Chalkboard XOR Game
LeetCode 810. Chalkboard XOR Game
Description
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:
Notes:
1 <= N <= 1000
.0 <= nums[i] <= 2^16
.
Tags
Math
Solution
Alice's winning conditions (any):
The length of
nums
is even;The XOR of all elements is 0.
Alice moves first. If the length of nums
is even, Alice can always find out a number and make the XOR of remaining numbers is non-zero after erasing picked one.
Complexity
Time complexity:
Space complexity:
Code
Reference
Last updated
Was this helpful?