{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "442117a940ec48eb9cd2269475ae2a1d",
  "keywords": "哈希表,字符串,回溯",
  "title": "电话号码的字母组合",
  "desc": [
    {
      "content": "\n<p>给定一个仅包含数字 <code>2-9</code> 的字符串，返回所有它能表示的字母组合。答案可以按 <strong>任意顺序</strong> 返回。</p><p>给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。</p><p><img src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0017.Letter%20Combinations%20of%20a%20Phone%20Number/images/17_telephone_keypad.png\" style=\"width: 200px;\" /></p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>digits = \"23\"<strong><br />输出：</strong>[\"ad\",\"ae\",\"af\",\"bd\",\"be\",\"bf\",\"cd\",\"ce\",\"cf\"]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>digits = \"\"<strong><br />输出：</strong>[]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>digits = \"2\"<strong><br />输出：</strong>[\"a\",\"b\",\"c\"]</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>0 <= digits.length <= 4</code></li>\t<li><code>digits[i]</code> 是范围 <code>['2', '9']</code> 的一个数字。</li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic List<String> letterCombinations(String digits) {\n\t\tCharacter[][] letters = { {}, {}, { 'a', 'b', 'c' }, { 'd', 'e', 'f' }, { 'g', 'h', 'i' }, { 'j', 'k', 'l' },\n\t\t\t\t{ 'm', 'n', 'o' }, { 'p', 'q', 'r', 's' }, { 't', 'u', 'v' }, { 'w', 'x', 'y', 'z' }, };\n\t\tList<List<Character>> combinations = new ArrayList<>();\n\t\tfor (int i = 0; i < digits.length(); i++) {\n\t\t\tCharacter d = digits.charAt(i);\n\t\t\tint index = Character.getNumericValue(d);\n\t\t\tCharacter[] letter = letters[index];\n\t\t\tSystem.out.println(d);\n\t\t\tif (i == 0) {\n\t\t\t\tfor (int j = 0; j < letter.length; j++) {\n\t\t\t\t\tList<Character> c = new ArrayList<>();\n\t\t\t\t\tc.add(letter[j]);\n\t\t\t\t\tcombinations.add(c);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tList<List<Character>> added = new ArrayList<>();\n\t\t\t\tfor (int j = 0; j < combinations.size(); j++) {\n\t\t\t\t\tList<Character> c = combinations.get(j);\n\t\t\t\t\tList<Character> origin_c = new ArrayList<>(c);\n\t\t\t\t\tfor (int k = 0; k < letter.length; k++) {\n\t\t\t\t\t\tCharacter l = letter[k];\n\t\t\t\t\t\tif (k == 0) {\n\t\t\t\t\t\t\tc.add(l);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tList<Character> new_c = new ArrayList<>(origin_c);\n\t\t\t\t\t\t\tnew_c.add(l);\n\t\t\t\t\t\t\tadded.add(new_c);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcombinations.addAll(added);\n\t\t\t}\n\t\t}\n\t\tList<String> output = new ArrayList<>();\n\t\tfor (int i = 0; i < combinations.size(); i++) {\n\t\t\tList<Character> c = combinations.get(i);\n\t\t\tStringBuilder sb = new StringBuilder();\n\t\t\tfor (Character l : c) {\n\t\t\t\tsb.append(l);\n\t\t\t}\n\t\t\toutput.add(sb.toString());\n\t\t}\n\t\treturn output;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-cdfa1a7c19ef4e879fcab664e5c47371",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600471003"
}