{"id":8011,"date":"2021-01-23T14:08:09","date_gmt":"2021-01-23T22:08:09","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8011"},"modified":"2021-01-23T14:10:54","modified_gmt":"2021-01-23T22:10:54","slug":"leetcode-1734-decode-xored-permutation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1734-decode-xored-permutation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1734. Decode XORed Permutation"},"content":{"rendered":"\n<p>There is an integer array&nbsp;<code>perm<\/code>&nbsp;that is a permutation of the first&nbsp;<code>n<\/code>&nbsp;positive integers, where&nbsp;<code>n<\/code>&nbsp;is always&nbsp;<strong>odd<\/strong>.<\/p>\n\n\n\n<p>It was encoded into another integer array&nbsp;<code>encoded<\/code>&nbsp;of length&nbsp;<code>n - 1<\/code>, such that&nbsp;<code>encoded[i] = perm[i] XOR perm[i + 1]<\/code>. For example, if&nbsp;<code>perm = [1,3,2]<\/code>, then&nbsp;<code>encoded = [2,1]<\/code>.<\/p>\n\n\n\n<p>Given the&nbsp;<code>encoded<\/code>&nbsp;array, return&nbsp;<em>the original array<\/em>&nbsp;<code>perm<\/code>. It is guaranteed that the answer exists and is unique.<\/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> encoded = [3,1]\n<strong>Output:<\/strong> [1,2,3]\n<strong>Explanation:<\/strong> If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]\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> encoded = [6,5,4,6]\n<strong>Output:<\/strong> [2,4,1,5,3]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= n &lt;&nbsp;10<sup>5<\/sup><\/code><\/li><li><code>n<\/code>&nbsp;is odd.<\/li><li><code>encoded.length == n - 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: XOR<\/strong><\/h2>\n\n\n\n<p>The key is to find p[0]. p[i] = p[i &#8211; 1] ^ encoded[i &#8211; 1]<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>p[0] ^ p[1] ^ &#8230; ^ p[n-1] = 1 ^ 2 ^ &#8230; ^ n<\/li><li>encoded[1] ^ encode[3] ^ &#8230; ^ encoded[n-2] = (p[1] ^ p[2]) ^ (p[3] ^ p[4]) ^ &#8230; ^ (p[n-2] ^ p[n-1])<\/li><\/ol>\n\n\n\n<p>1) xor 2) = p[0]<\/p>\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  vector<int> decode(vector<int>& encoded) {    \n    const int n = encoded.size() + 1;\n    vector<int> perm(n);\n    \/\/ p[0] = (p[0]^p[1]^...^p[n-1] = 1^2^...^n) \n    \/\/      ^ (p[1]^p[2]^...^p[n-1])\n    for (int i = 1; i <= n; ++i) \n      perm[0] ^= i;\n    for (int i = 1; i < n; i += 2)\n      perm[0] ^= encoded[i];\n    for (int i = 1; i < n; ++i)\n      perm[i] = perm[i - 1] ^ encoded[i - 1];\n    return perm;        \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is an integer array&nbsp;perm&nbsp;that is a permutation of the first&nbsp;n&nbsp;positive integers, where&nbsp;n&nbsp;is always&nbsp;odd. It was encoded into another integer array&nbsp;encoded&nbsp;of length&nbsp;n &#8211; 1, such&#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],"tags":[16,63],"class_list":["post-8011","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8011","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=8011"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8011\/revisions"}],"predecessor-version":[{"id":8014,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8011\/revisions\/8014"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}