{"id":1043,"date":"2025-12-25T04:10:21","date_gmt":"2025-12-24T20:10:21","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1043"},"modified":"2025-12-25T04:10:21","modified_gmt":"2025-12-24T20:10:21","slug":"leetcode-416-%e5%88%86%e5%89%b2%e7%ad%89%e5%92%8c%e5%ad%90%e9%9b%86","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=1043","title":{"rendered":"LeetCode 416 \u2013 \u5206\u5272\u7b49\u548c\u5b50\u96c6"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a \u53ea\u5305\u542b\u6b63\u6574\u6570 \u7684 \u975e\u7a7a \u6570\u7ec4 nums \u3002\u8bf7\u4f60\u5224\u65ad\u662f\u5426\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u6570\u7ec4\u5206\u5272\u6210\u4e24\u4e2a\u5b50\u96c6\uff0c\u4f7f\u5f97\u4e24\u4e2a\u5b50\u96c6\u7684\u5143\u7d20\u548c\u76f8\u7b49\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1anums = [1,5,11,5]<br \/>\n\u8f93\u51fa\uff1atrue<br \/>\n\u89e3\u91ca\uff1a\u6570\u7ec4\u53ef\u4ee5\u5206\u5272\u6210 [1, 5, 5] \u548c [11] \u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u9898\u76ee\u4e2d\u9690\u542b\u4e86\u4e00\u4e2a\u4fe1\u606f\uff0c\u201c\u4e24\u4e2a\u5b50\u96c6\u7684\u5143\u7d20\u548c\u76f8\u7b49\u201d\uff0c\u610f\u5473\u7740\u6bcf\u4e2a\u5b50\u96c6\u90fd\u662f sum\/2\u3002<br \/>\n\u95ee\u9898\u53ef\u4ee5\u8f6c\u5316\u4e3a\uff0c\u4f7f\u7528\u6570\u7ec4\u5185\u7684\u5143\u7d20\u76f8\u52a0\uff0c\u662f\u5426\u53ef\u4ee5\u7ec4\u6210 sum\/2.\uff08\u6709\u4e00\u4e2a\u7ec4\u5408\u6210\u529f\uff0c\u5269\u4f59\u503c\u5fc5\u7136\u5c31\u662f\u53e6\u4e00\u4e2a sum\/2\uff09\u3002<br \/>\n\u60f3\u8c61\u4e00\u4e2a\u5bb9\u5668\u7528\u4e8e\u5b58\u50a8\u6240\u6709\u53ef\u80fd\u7684\u76f8\u52a0\u7ed3\u679c\uff0c\u6bcf\u6b21\u679a\u4e3e\u6570\u7ec4\u5143\u7d20\u65f6\uff0c\u9700\u8981\u4e0e\u5bb9\u5668\u5185\u5b58\u5728\u7684\u7ed3\u679c\u76f8\u52a0\uff0c\u7136\u540e\u91cd\u65b0\u653e\u5165\u5bb9\u5668\u3002<br \/>\n\u4ee5  [1,5,11,5] \u4e3a\u4f8b\uff1a<br \/>\n&#8211; \u7b2c1\u4e2a\u5143\u7d20 1\uff0c\u5bb9\u5668 [1]<br \/>\n&#8211; \u7b2c2\u4e2a\u5143\u7d20 5\uff0c\u5bb9\u5668 [1, 5, 6]<br \/>\n&#8211; \u7b2c3\u4e2a\u5143\u7d20 11\uff0c\u5bb9\u5668 [1, 5, 6, 11, 12, 16, 17]<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 dp[i] \u8868\u793a\u662f\u5426\u6709\u548c\u4e3a i \u7684\u7ed3\u679c\uff0c\u7136\u540e\u8fdb\u884c\u9012\u63a8<code>dp[i] = dp[i] || dp[i - \u6570\u7ec4\u5143\u7d20]<\/code><\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6 O(m * n)\uff0c\u5176\u4e2d m \u662f sum\/2\uff0cn \u662f\u5143\u7d20\u4e2a\u6570<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public boolean canPartition(int[] nums) {\n    int sum = 0;\n    for (int n : nums) {\n        sum += n;\n    }\n    if (sum % 2 == 1) {\n        return false;\n    }\n    int target = sum \/ 2;\n    boolean[] dp = new boolean[target + 1];\n    dp[0] = true;\n    for (int n : nums) {\n        for (int j = target; j &gt;= n; j--) {\n            dp[j] = dp[j] || dp[j - n];\n        }\n        if (dp[target]) {\n            return true;\n        }\n    }\n    return false;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun canPartition(nums: IntArray): Boolean {\n    val sum = nums.sum()\n    if (sum % 2 == 1) {\n        return false\n    }\n    val target = sum \/ 2\n    val dp = BooleanArray(target + 1)\n    dp[0] = true\n    for (n in nums) {\n        for (j in target downTo n) {\n            dp[j] = dp[j] || dp[j - n]\n        }\n        if (dp[target]) {\n            return true\n        }\n    }\n    return false\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a \u53ea\u5305\u542b\u6b63\u6574\u6570 \u7684 \u975e\u7a7a \u6570\u7ec4 nums \u3002\u8bf7\u4f60\u5224\u65ad\u662f\u5426\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u6570\u7ec4\u5206\u5272\u6210\u4e24\u4e2a\u5b50\u96c6\uff0c\u4f7f\u5f97\u4e24 [&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":[20,40],"class_list":["post-1043","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-40"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1043","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=1043"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1043\/revisions"}],"predecessor-version":[{"id":1044,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1043\/revisions\/1044"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}