{"id":2821,"date":"2018-05-02T19:52:50","date_gmt":"2018-05-03T02:52:50","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2821"},"modified":"2018-05-02T19:53:13","modified_gmt":"2018-05-03T02:53:13","slug":"leetcode-810-chalkboard-xor-game","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-810-chalkboard-xor-game\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 810. Chalkboard XOR Game"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>We are given non-negative integers nums[i] which are written on a chalkboard.\u00a0 Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.\u00a0 If erasing a number causes\u00a0the bitwise XOR of all the elements of the chalkboard to become\u00a00, then that player loses.\u00a0 (Also, we&#8217;ll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.)<\/p>\n<p>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.<\/p>\n<p>Return True if and only if Alice wins the game, assuming both players play optimally.<\/p>\n<pre class=\"crayon:false \"><strong>Example:<\/strong>\r\n<strong>Input:<\/strong> nums = [1, 1, 2]\r\n<strong>Output:<\/strong> false\r\n<strong>Explanation:<\/strong> \r\nAlice has two choices: erase 1 or erase 2. \r\nIf 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. \r\nIf 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.\r\n\r\n<\/pre>\n<p><strong>Notes:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= N &lt;= 1000<\/code>.<\/li>\n<li><code>0 &lt;= nums[i] &lt;= 2^16<\/code>.<\/li>\n<\/ul>\n<h1><strong>Solution: Math<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 15 ms\r\nclass Solution {\r\npublic:\r\n  bool xorGame(vector&lt;int&gt;&amp; nums) {    \r\n    if (nums.size() % 2 == 0) return true;\r\n    int x = 0;\r\n    for (int num : nums) x ^= num;\r\n    return x == 0;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem We are given non-negative integers nums[i] which are written on a chalkboard.\u00a0 Alice and Bob take turns erasing exactly one number from the chalkboard,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126,49],"tags":[16,217,31,63],"class_list":["post-2821","post","type-post","status-publish","format-standard","hentry","category-bit","category-math","tag-bit","tag-hard","tag-math","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2821"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2821\/revisions"}],"predecessor-version":[{"id":2824,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2821\/revisions\/2824"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}