{"id":969,"date":"2025-12-15T00:05:00","date_gmt":"2025-12-14T16:05:00","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=969"},"modified":"2025-12-15T00:05:00","modified_gmt":"2025-12-14T16:05:00","slug":"leetcode-78-%e5%ad%90%e9%9b%86","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=969","title":{"rendered":"LeetCode 78 &#8211; \u5b50\u96c6"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20 \u4e92\u4e0d\u76f8\u540c \u3002\u8fd4\u56de\u8be5\u6570\u7ec4\u6240\u6709\u53ef\u80fd\u7684\u5b50\u96c6\uff08\u5e42\u96c6\uff09\u3002<br \/>\n\u89e3\u96c6 \u4e0d\u80fd \u5305\u542b\u91cd\u590d\u7684\u5b50\u96c6\u3002\u4f60\u53ef\u4ee5\u6309 \u4efb\u610f\u987a\u5e8f \u8fd4\u56de\u89e3\u96c6\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1anums = [1,2,3]<br \/>\n\u8f93\u51fa\uff1a[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u672c\u8d28\u662f\u7ec4\u5408\uff0c\u662f\u65e0\u5e8f\u7684\uff0c\u800c\u6392\u5217\u662f\u6709\u5e8f\u7684\u3002<br \/>\n\u4f8b\u5982 [1, 2] \u548c [2, 1]\uff0c\u89c6\u4e3a\u540c\u4e00\u4e2a\u7ed3\u679c\u3002<br \/>\n\u5982\u679c\u76f4\u63a5\u6309\u7167\u6392\u5217\u7684 DFS \u6765\u505a\uff0c\u4f1a\u4ea7\u751f\u5f88\u591a\u987a\u5e8f\u4e0d\u540c\u7684\u7ed3\u679c\uff0c\u9700\u8981\u989d\u5916\u7684\u6210\u672c\u505a\u53bb\u91cd\u3002<br \/>\n\u53ef\u4ee5\u901a\u8fc7\u5728 DFS \u4e2d\u5f15\u5165\u4e00\u4e2a index \u6765\u89e3\u51b3\u91cd\u590d\u95ee\u9898\uff0c\u8981\u6c42\u4e0b\u4e00\u4e2a\u7ed3\u70b9\u7684\u4f4d\u7f6e\u5728 index \u4e4b\u540e\uff0c\u4e5f\u5c31\u662f\u4e0d\u8bb8\u5411\u5f53\u524d\u7684\u5de6\u4fa7\u641c\u7d22\uff0c\u53ea\u80fd\u5411\u53f3\u4fa7\u641c\u7d22\u3002<br \/>\n\u8fd9\u6837\u5c31\u4e0d\u4f1a\u4ea7\u751f\u91cd\u590d\u7684\u641c\u7d22\uff0c\u6548\u7387\u6700\u9ad8\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">private List&lt;List&lt;Integer&gt;&gt; ans = new ArrayList&lt;&gt;();\nprivate List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\n\npublic List&lt;List&lt;Integer&gt;&gt; subsets(int[] nums) {\n    dfs(nums, -1);\n    return ans;\n}\n\nprivate void dfs(int[] nums, int index) {\n    ans.add(new ArrayList&lt;&gt;(list));\n    for (int i = index + 1; i &lt; nums.length; i++) {\n        Integer x = nums[i];\n        list.add(x);\n        dfs(nums, i);\n        list.remove(x);\n    }\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">private val ans = ArrayList&lt;List&lt;Int&gt;&gt;()\nprivate val list = ArrayList&lt;Int&gt;()\n\nfun subsets(nums: IntArray): List&lt;List&lt;Int&gt;&gt; {\n    dfs(nums, -1)\n    return ans\n}\n\nprivate fun dfs(nums: IntArray, index: Int) {\n    ans.add(ArrayList(list))\n    if (index == nums.size - 1) {\n        return\n    }\n    for (i in index + 1 until nums.size) {\n        val x = nums[i]\n        list.add(x)\n        dfs(nums, i)\n        list.remove(x)\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20 \u4e92\u4e0d\u76f8\u540c \u3002\u8fd4\u56de\u8be5\u6570\u7ec4\u6240\u6709\u53ef\u80fd\u7684\u5b50\u96c6\uff08\u5e42\u96c6\uff09\u3002 \u89e3\u96c6 \u4e0d [&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,91],"class_list":["post-969","post","type-post","status-publish","format-standard","hentry","category-algo","tag-dfs","tag-leetcode","tag-90","tag-91"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/969","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=969"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":970,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/969\/revisions\/970"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}