810. Chalkboard XOR Game

标签: leetcode-java
15人阅读 评论(0) 收藏 举报
分类:

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.

class Solution {
    public boolean xorGame(int[] nums) {
        int x = 0;
        for (int v : nums) 
            x ^= v;
        return x == 0 || nums.length % 2 == 0;
    }
}
查看评论

Hacking the Game——聊聊黑客的三观

360开源大会不仅邀请10多位360内部技术专家,还特邀Apache基金会副总裁、触控未来CEO、华为和Intel等多位开源专家共聚大会,畅聊开源!
  • 2016年05月30日 11:08

leetcode 810. Chalkboard XOR Game

leetcode 810. Chalkboard XOR Game 原题地址:https://leetcode.com/problems/chalkboard-xor-game/ 题目 ...
  • birdreamer
  • birdreamer
  • 2018-04-01 20:04:15
  • 44

810-Chalkboard XOR Game

Description: We are given non-negative integers nums[i] which are written on a chalkboard. Alice a...
  • LaputaFallen
  • LaputaFallen
  • 2018-04-01 12:57:11
  • 58

808. Soup Servings

There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There a...
  • zjucor
  • zjucor
  • 2018-04-01 11:08:18
  • 76

[Leetcode] 810. Chalkboard XOR Game 解题报告

题目: We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bo...
  • magicbean2
  • magicbean2
  • 2018-04-09 10:14:58
  • 32

[Leetcode] 808. Soup Servings 解题报告

题目: There are two types of soup: type A and type B. Initially we have N ml of each type of soup. ...
  • magicbean2
  • magicbean2
  • 2018-04-09 09:10:56
  • 42

Chalkboard XOR Game

We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take tu...
  • u012876995
  • u012876995
  • 2018-04-04 22:35:26
  • 9

[xor最小生成树 分治 字典树合并] BNUOJ52318 Be Friends

二进制从高到低分治,当前分治到第 ii 位那么肯定是把第 ii 位为0的放一起,为1的放一起,这两块分别成一个联通块,然后之间连一条边是最优的两块分治下去。找最小边的话一边建字典树,另一边的数在字典树...
  • Coldef
  • Coldef
  • 2017-10-23 20:06:40
  • 202

Codeforces 276D Little Girl and Maximum XOR (贪心)

Codeforces 276D Little Girl and Maximum XOR (贪心)
  • Tc_To_Top
  • Tc_To_Top
  • 2016-07-09 22:48:33
  • 777

编程之美中的NIM游戏及异或性质应用

最近看《编程之美》一书,感叹思维之妙,不过看过之后又在很多数学方面的书里面找到了同样的解法例如《组合数学》、《图论导引》等,之后才知道其实很多书上的算法都是源自数学、图论、博弈论知识,同时也一直有一种...
  • ingwfj
  • ingwfj
  • 2016-09-09 01:05:16
  • 2256
    个人资料
    持之以恒
    等级:
    访问量: 3万+
    积分: 6422
    排名: 4784
    最新评论