{"id":6953,"date":"2020-06-20T21:57:38","date_gmt":"2020-06-21T04:57:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6953"},"modified":"2020-06-20T21:57:57","modified_gmt":"2020-06-21T04:57:57","slug":"leetcode-1486-xor-operation-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1486-xor-operation-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1486. XOR Operation in an Array"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>&nbsp;and an integer&nbsp;<code>start<\/code>.<\/p>\n\n\n\n<p>Define an array&nbsp;<code>nums<\/code>&nbsp;where&nbsp;<code>nums[i] = start + 2*i<\/code>&nbsp;(0-indexed) and&nbsp;<code>n == nums.length<\/code>.<\/p>\n\n\n\n<p>Return the bitwise&nbsp;XOR&nbsp;of all elements of&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, start = 0\n<strong>Output:<\/strong> 8\n<strong>Explanation: <\/strong>Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8.\nWhere \"^\" corresponds to bitwise XOR operator.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, start = 3\n<strong>Output:<\/strong> 8\n<strong>Explanation: <\/strong>Array nums is equal to [3, 5, 7, 9] where (3 ^ 5 ^ 7 ^ 9) = 8.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 1, start = 7\n<strong>Output:<\/strong> 7\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 10, start = 5\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><li><code>0 &lt;= start &lt;= 1000<\/code><\/li><li><code>n == nums.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int xorOperation(int n, int start) {\n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      ans ^= (start + 2 * i);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n&nbsp;and an integer&nbsp;start. Define an array&nbsp;nums&nbsp;where&nbsp;nums[i] = start + 2*i&nbsp;(0-indexed) and&nbsp;n == nums.length. Return the bitwise&nbsp;XOR&nbsp;of all elements of&nbsp;nums. Example 1: Input: n&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,179,63],"class_list":["post-6953","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-simulation","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6953","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=6953"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6953\/revisions"}],"predecessor-version":[{"id":6955,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6953\/revisions\/6955"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}