LintCode——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.

样例:

Input: nums = [1, 1, 2]
Output: false

Python:

 1 class Solution:
 2     """
 3     @param nums: a list of integers
 4     @return: return a boolean
 5     """
 6     def xorGame(self, nums):
 7         # write your code here
 8         result = 0
 9         for i in nums: 
10             result = result ^ i
11         if result == 0 or len(nums) % 2 == 0:
12             return True
13         else:
14             return False

猜你喜欢

转载自www.cnblogs.com/wangcj2015/p/9095972.html
xor