“febdb369e72bcc06c2755647c9ca3320fd6671f7”上不存在“pages/API/git@gitcode.net:Accustomed_/hello-uni-app-x.git”
solution.json 2.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "3592b4ef75784657938f540c1b5fadc9",
  "keywords": "位运算,数学,回溯",
  "title": "格雷编码",
  "desc": [
    {
      "content": "\n<p>格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。</p>\n<p>给定一个代表编码总位数的非负整数<em> n</em>,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。</p>\n<p>格雷编码序列必须以 0 开头。</p>\n<p>&nbsp;</p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong>&nbsp;2<strong><br />输出:</strong>&nbsp;[0,1,3,2]<strong><br />解释:</strong>00 - 001 - 111 - 310 - 2对于给定的&nbsp;<em>n</em>,其格雷编码序列并不唯一。例如,[0,2,3,1]&nbsp;也是一个有效的格雷编码序列。00 - 010 - 211 - 301 - 1</pre>\n<p><strong>示例&nbsp;2:</strong></p>\n<pre><strong>输入:</strong>&nbsp;0<strong><br />输出:</strong>&nbsp;[0]<strong><br />解释:</strong> 我们定义格雷编码序列必须以 0 开头。给定编码总位数为 <em>n</em> 的格雷编码序列,其长度为 2<sup>n</sup>。当 <em>n</em> = 0 时,长度为 2<sup>0</sup> = 1。因此,当 <em>n</em> = 0 时,其格雷编码序列为 [0]。</pre>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic List<Integer> grayCode(int n) {\n\t\tList<Integer> res = new ArrayList<>();\n\t\tres.add(0);\n\t\tint cur;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint change = 1 << i;\n\t\t\tcur = res.size() - 1;\n\t\t\twhile (cur >= 0) {\n\t\t\t\tres.add(res.get(cur) ^ change);\n\t\t\t\tcur--;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-7fa5cb6f690545fc865aece3cd1a74a9",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470797"
}