{"id":971,"date":"2025-12-15T16:43:10","date_gmt":"2025-12-15T08:43:10","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=971"},"modified":"2025-12-15T16:43:10","modified_gmt":"2025-12-15T08:43:10","slug":"leetcode-17-%e7%94%b5%e8%af%9d%e5%8f%b7%e7%a0%81%e7%9a%84%e5%ad%97%e6%af%8d%e7%bb%84%e5%90%88","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=971","title":{"rendered":"LeetCode 17 &#8211; \u7535\u8bdd\u53f7\u7801\u7684\u5b57\u6bcd\u7ec4\u5408"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4ec5\u5305\u542b\u6570\u5b57 2-9 \u7684\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u6240\u6709\u5b83\u80fd\u8868\u793a\u7684\u5b57\u6bcd\u7ec4\u5408\u3002\u7b54\u6848\u53ef\u4ee5\u6309 \u4efb\u610f\u987a\u5e8f \u8fd4\u56de\u3002<br \/>\n\u7ed9\u51fa\u6570\u5b57\u5230\u5b57\u6bcd\u7684\u6620\u5c04\u5982\u4e0b\uff08\u4e0e\u7535\u8bdd\u6309\u952e\u76f8\u540c\uff09\u3002\u6ce8\u610f 1 \u4e0d\u5bf9\u5e94\u4efb\u4f55\u5b57\u6bcd\u3002<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/12\/wp_editor_md_239978a2e71ef98b0fa29268726d21a3.jpg\"><img decoding=\"async\" src=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/12\/wp_editor_md_239978a2e71ef98b0fa29268726d21a3.jpg\" alt=\"\" \/><\/a><\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1adigits = &#8220;23&#8221;<br \/>\n\u8f93\u51fa\uff1a[&#8220;ad&#8221;,&#8221;ae&#8221;,&#8221;af&#8221;,&#8221;bd&#8221;,&#8221;be&#8221;,&#8221;bf&#8221;,&#8221;cd&#8221;,&#8221;ce&#8221;,&#8221;cf&#8221;]<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>DFS\uff0c\u6bcf\u5c42\u5904\u7406\u4e00\u4e2a\u6309\u952e<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">private List&lt;List&lt;Character&gt;&gt; data = new ArrayList&lt;&gt;();\nprivate List&lt;String&gt; ans = new ArrayList&lt;&gt;();\nprivate List&lt;Character&gt; list = new ArrayList&lt;&gt;();\n\npublic List&lt;String&gt; letterCombinations(String digits) {\n    data.add(new ArrayList&lt;&gt;());\n    data.add(new ArrayList&lt;&gt;());\n    data.add(Arrays.asList('a', 'b', 'c'));\n    data.add(Arrays.asList('d', 'e', 'f'));\n    data.add(Arrays.asList('g', 'h', 'i'));\n    data.add(Arrays.asList('j', 'k', 'l'));\n    data.add(Arrays.asList('m', 'n', 'o'));\n    data.add(Arrays.asList('p', 'q', 'r', 's'));\n    data.add(Arrays.asList('t', 'u', 'v'));\n    data.add(Arrays.asList('w', 'x', 'y', 'z'));\n\n    dfs(digits, 0);\n    return ans;\n}\n\nprivate void dfs(String digits, int index) {\n    if (index == digits.length()) {\n        StringBuilder sb = new StringBuilder();\n        for (Character ch : list) {\n            sb.append(ch);\n        }\n        ans.add(sb.toString());\n        return;\n    }\n\n    int num = digits.charAt(index) - '0';\n    List&lt;Character&gt; keys = data.get(num);\n    for (Character ch : keys) {\n        list.add(ch);\n        dfs(digits, index + 1);\n        list.remove(list.size() - 1);\n    }\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">private val map = arrayOf(\n    listOf(),\n    listOf(),\n    listOf('a', 'b', 'c'),\n    listOf('d', 'e', 'f'),\n    listOf('g', 'h', 'i'),\n    listOf('j', 'k', 'l'),\n    listOf('m', 'n', 'o'),\n    listOf('p', 'q', 'r', 's'),\n    listOf('t', 'u', 'v'),\n    listOf('w', 'x', 'y', 'z')\n)\nprivate val ans = ArrayList&lt;String&gt;()\nprivate val list = ArrayList&lt;Char&gt;()\n\nfun letterCombinations(digits: String): List&lt;String&gt; {\n    dfs(digits, 0)\n    return ans\n}\n\nprivate fun dfs(digits: String, index: Int) {\n    if (index == digits.length) {\n        val sb = StringBuilder()\n        for (ch in list) {\n            sb.append(ch)\n        }\n        ans.add(sb.toString())\n        return\n    }\n    val num = digits[index] - '0'\n    val chs = map[num] ?: return\n    for (ch in chs) {\n        list.add(ch)\n        dfs(digits, index + 1)\n        list.removeLast()\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u5b9a\u4e00\u4e2a\u4ec5\u5305\u542b\u6570\u5b57 2-9 \u7684\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u6240\u6709\u5b83\u80fd\u8868\u793a\u7684\u5b57\u6bcd\u7ec4\u5408\u3002\u7b54\u6848\u53ef\u4ee5\u6309 \u4efb\u610f\u987a\u5e8f \u8fd4\u56de\u3002 \u7ed9\u51fa [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[11,20,90],"class_list":["post-971","post","type-post","status-publish","format-standard","hentry","category-algo","tag-dfs","tag-leetcode","tag-90"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=971"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/971\/revisions"}],"predecessor-version":[{"id":972,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/971\/revisions\/972"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}