{"id":93658,"date":"2026-08-12T23:24:54","date_gmt":"2026-08-12T15:24:54","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/93658.html"},"modified":"2026-08-12T23:24:54","modified_gmt":"2026-08-12T15:24:54","slug":"leetcode-39%ef%bc%9a%e7%bb%84%e5%90%88%e6%80%bb%e5%92%8c-java-dfs-%e5%9b%9e%e6%ba%af%e4%b8%8e%e5%89%aa%e6%9e%9d%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/93658.html","title":{"rendered":"LeetCode 39\uff1a\u7ec4\u5408\u603b\u548c\u2014\u2014Java DFS \u56de\u6eaf\u4e0e\u526a\u679d\u8be6\u89e3"},"content":{"rendered":"<h3>\u4e00\u3001\u9898\u76ee\u63cf\u8ff0<\/h3>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u91cd\u590d\u5143\u7d20\u7684\u6b63\u6574\u6570\u6570\u7ec4 candidates \u548c\u4e00\u4e2a\u76ee\u6807\u6574\u6570 target&#xff0c;\u627e\u51fa\u6240\u6709\u6570\u5b57\u4e4b\u548c\u7b49\u4e8e target \u7684\u4e0d\u540c\u7ec4\u5408\u3002<\/p>\n<p>\u6570\u7ec4\u4e2d\u7684\u540c\u4e00\u4e2a\u6570\u5b57\u53ef\u4ee5\u88ab\u91cd\u590d\u9009\u62e9\u3002\u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u6570\u5b57\u7684\u9009\u62e9\u6b21\u6570\u4e0d\u540c&#xff0c;\u5c31\u8ba4\u4e3a\u662f\u4e0d\u540c\u7ec4\u5408\u3002\u9898\u76ee\u5141\u8bb8\u4ee5\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002<\/p>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<p>\u8f93\u5165&#xff1a;candidates &#061; [2, 3, 6, 7], target &#061; 7<br \/>\n\u8f93\u51fa&#xff1a;[[2, 2, 3], [7]]<\/p>\n<p>\u6570\u5b57 2 \u53ef\u4ee5\u91cd\u590d\u4f7f\u7528&#xff0c;\u56e0\u6b64\u53ef\u4ee5\u5f97\u5230\u7ec4\u5408 [2,2,3]&#xff1b;\u6570\u5b57 7 \u672c\u8eab\u4e5f\u7b49\u4e8e\u76ee\u6807\u503c&#xff0c;\u6240\u4ee5 [7] \u4e5f\u662f\u4e00\u4e2a\u6709\u6548\u7ec4\u5408\u3002<\/p>\n<p>\u8fd9\u9053\u9898\u7684\u96be\u70b9\u4e3b\u8981\u6709\u4e24\u4e2a&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u540c\u4e00\u4e2a\u6570\u5b57\u53ef\u4ee5\u65e0\u9650\u6b21\u4f7f\u7528&#xff1b;<\/p>\n<\/li>\n<li>\n<p>[2,2,3] \u548c [3,2,2] \u53ea\u80fd\u7b97\u4f5c\u4e00\u79cd\u7ec4\u5408\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u89e3\u51b3\u8fd9\u4e24\u4e2a\u95ee\u9898\u7684\u5173\u952e&#xff0c;\u5c31\u662f DFS\u3001\u56de\u6eaf\u548c start \u8d77\u59cb\u4e0b\u6807\u3002<\/p>\n<h3>\u4e8c\u3001\u628a\u7ec4\u5408\u8fc7\u7a0b\u770b\u6210\u4e00\u68f5\u51b3\u7b56\u6811<\/h3>\n<p>\u5047\u8bbe&#xff1a;<\/p>\n<p>candidates &#061; [3, 4, 5], target &#061; 9<\/p>\n<p>\u4ece\u7a7a\u7ec4\u5408\u5f00\u59cb&#xff0c;\u7b2c\u4e00\u8f6e\u53ef\u4ee5\u9009\u62e9 3\u30014 \u6216 5\u3002\u9009\u62e9\u4e00\u4e2a\u6570\u5b57\u540e&#xff0c;\u518d\u7ee7\u7eed\u9009\u62e9\u4e0b\u4e00\u4e2a\u6570\u5b57&#xff0c;\u76f4\u5230\u5f53\u524d\u603b\u548c\u7b49\u4e8e\u6216\u8005\u8d85\u8fc7 target\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u5148\u9009\u62e9 3&#xff0c;\u5f53\u524d\u8def\u5f84\u4e3a [3]&#xff0c;\u603b\u548c\u4e3a 3\u3002\u56e0\u4e3a\u6570\u5b57\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528&#xff0c;\u4e0b\u4e00\u5c42\u4ecd\u7136\u53ef\u4ee5\u9009\u62e9 3&#xff0c;\u5f97\u5230 [3,3]&#xff1b;\u7ee7\u7eed\u9009\u62e9 3 \u540e\u5f97\u5230 [3,3,3]&#xff0c;\u603b\u548c\u6b63\u597d\u4e3a 9&#xff0c;\u56e0\u6b64\u627e\u5230\u4e00\u4e2a\u6709\u6548\u7ec4\u5408\u3002<\/p>\n<p>\u5982\u679c\u5148\u9009\u62e9 4&#xff0c;\u518d\u9009\u62e9 5&#xff0c;\u53ef\u4ee5\u5f97\u5230 [4,5]\u3002\u4f46\u5982\u679c\u7b2c\u4e00\u8f6e\u9009\u62e9 5&#xff0c;\u7b2c\u4e8c\u8f6e\u518d\u9009\u62e9 4&#xff0c;\u5c31\u4f1a\u5f97\u5230 [5,4]\u3002\u4e24\u8005\u5305\u542b\u7684\u6570\u5b57\u548c\u6570\u91cf\u5b8c\u5168\u76f8\u540c&#xff0c;\u672c\u8d28\u4e0a\u662f\u540c\u4e00\u79cd\u7ec4\u5408&#xff0c;\u4e0d\u80fd\u91cd\u590d\u52a0\u5165\u7b54\u6848\u3002<\/p>\n<p>\u56e0\u6b64&#xff0c;\u6211\u4eec\u4e0d\u80fd\u8ba9\u6bcf\u4e00\u5c42\u90fd\u4ece\u6570\u7ec4\u4e0b\u6807 0 \u5f00\u59cb\u904d\u5386&#xff0c;\u800c\u8981\u901a\u8fc7\u4e00\u4e2a start \u53c2\u6570\u63a7\u5236\u4e0b\u4e00\u5c42\u7684\u9009\u62e9\u8303\u56f4\u3002<\/p>\n<h3>\u4e09\u3001start \u5982\u4f55\u907f\u514d\u91cd\u590d\u7ec4\u5408<\/h3>\n<p>start \u8868\u793a\u5f53\u524d\u8fd9\u4e00\u5c42\u53ef\u4ee5\u4ece candidates \u7684\u54ea\u4e2a\u4f4d\u7f6e\u5f00\u59cb\u9009\u62e9\u3002<\/p>\n<p>for (int i &#061; start; i &lt; candidates.length; i&#043;&#043;) {<br \/>\n    \/\/ \u9009\u62e9 candidates[i]<br \/>\n}<\/p>\n<p>\u5047\u8bbe\u5f53\u524d\u7b2c\u4e00\u6b21\u9009\u62e9\u7684\u662f\u4e0b\u6807 1 \u5bf9\u5e94\u7684\u6570\u5b57 4&#xff0c;\u90a3\u4e48\u540e\u7eed\u53ea\u80fd\u7ee7\u7eed\u9009\u62e9\u4e0b\u6807\u5927\u4e8e\u6216\u7b49\u4e8e 1 \u7684\u6570\u5b57&#xff0c;\u5373 4 \u548c 5&#xff0c;\u4e0d\u80fd\u518d\u56de\u5934\u9009\u62e9\u4e0b\u6807 0 \u7684\u6570\u5b57 3\u3002<\/p>\n<p>\u8fd9\u6837\u4e00\u6765&#xff0c;\u53ef\u4ee5\u751f\u6210 [4,5]&#xff0c;\u4f46\u4e0d\u4f1a\u518d\u751f\u6210\u987a\u5e8f\u76f8\u53cd\u7684 [5,4]\u3002\u6240\u6709\u7ec4\u5408\u90fd\u4f1a\u6309\u7167\u5019\u9009\u6570\u7ec4\u4e2d\u7684\u4e0b\u6807\u987a\u5e8f\u6784\u9020&#xff0c;\u4ece\u800c\u81ea\u7136\u907f\u514d\u91cd\u590d\u3002<\/p>\n<p>\u8fd9\u91cc\u8981\u533a\u5206\u4e24\u4e2a\u5bb9\u6613\u6df7\u6dc6\u7684\u6982\u5ff5&#xff1a;<\/p>\n<ul>\n<li>\n<p>start \u9650\u5236\u7684\u662f\u5f53\u524d\u5c42\u53ef\u4ee5\u9009\u62e9\u7684\u8d77\u70b9&#xff0c;\u7528\u6765\u907f\u514d\u4e0d\u540c\u6392\u5217\u4ea7\u751f\u91cd\u590d\u7ec4\u5408&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u9012\u5f52\u65f6\u4f20\u5165 i&#xff0c;\u800c\u4e0d\u662f i &#043; 1&#xff0c;\u7528\u6765\u5141\u8bb8\u5f53\u524d\u6570\u5b57\u88ab\u91cd\u590d\u4f7f\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6838\u5fc3\u9012\u5f52\u8c03\u7528\u5982\u4e0b&#xff1a;<\/p>\n<p>traverseTree(candidates, target, i);<\/p>\n<p>\u5982\u679c\u4f20\u5165 i &#043; 1&#xff0c;\u5f53\u524d\u6570\u5b57\u5728\u4e0b\u4e00\u5c42\u5c31\u4e0d\u80fd\u518d\u6b21\u88ab\u9009\u62e9&#xff0c;\u8fd9\u5c06\u53d8\u6210\u201c\u6bcf\u4e2a\u6570\u5b57\u6700\u591a\u4f7f\u7528\u4e00\u6b21\u201d\u7684\u53e6\u4e00\u7c7b\u7ec4\u5408\u95ee\u9898\u3002<\/p>\n<p>\u53ef\u4ee5\u628a\u8fd9\u6761\u89c4\u5f8b\u8bb0\u6210\u4e00\u53e5\u8bdd&#xff1a;<\/p>\n<p>start \u8d1f\u8d23\u53bb\u91cd&#xff0c;\u4f20\u5165 i \u8d1f\u8d23\u590d\u7528\u3002<\/p>\n<h3>\u56db\u3001\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u4e0e\u526a\u679d<\/h3>\n<p>\u641c\u7d22\u8fc7\u7a0b\u4e2d\u9700\u8981\u6839\u636e\u5f53\u524d\u8def\u5f84\u603b\u548c sum \u5224\u65ad\u662f\u5426\u7ee7\u7eed\u9012\u5f52\u3002<\/p>\n<h4>1. \u627e\u5230\u6709\u6548\u7ec4\u5408<\/h4>\n<p>\u5f53 sum &#061;&#061; target \u65f6&#xff0c;\u8bf4\u660e\u5f53\u524d\u8def\u5f84\u662f\u4e00\u7ec4\u6709\u6548\u7b54\u6848&#xff1a;<\/p>\n<p>if (sum &#061;&#061; target) {<br \/>\n    res.add(new ArrayList&lt;&gt;(route));<br \/>\n    return;<br \/>\n}<\/p>\n<p>\u8fd9\u91cc\u5fc5\u987b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 ArrayList&#xff0c;\u4fdd\u5b58\u5f53\u524d\u8def\u5f84\u7684\u5feb\u7167\u3002\u4e0d\u80fd\u76f4\u63a5\u5c06 route \u653e\u5165\u7ed3\u679c\u96c6&#xff0c;\u56e0\u4e3a route \u5728\u540e\u7eed\u56de\u6eaf\u8fc7\u7a0b\u4e2d\u8fd8\u4f1a\u7ee7\u7eed\u4fee\u6539\u3002<\/p>\n<h4>2. \u5f53\u524d\u603b\u548c\u8d85\u8fc7\u76ee\u6807\u503c<\/h4>\n<p>\u5f53 sum &gt; target \u65f6&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u7ed3\u675f\u5f53\u524d\u5206\u652f&#xff1a;<\/p>\n<p>if (sum &gt; target) {<br \/>\n    return;<br \/>\n}<\/p>\n<p>\u9898\u76ee\u4fdd\u8bc1\u5019\u9009\u6570\u5b57\u90fd\u662f\u6b63\u6574\u6570\u3002\u603b\u548c\u4e00\u65e6\u8d85\u8fc7 target&#xff0c;\u7ee7\u7eed\u6dfb\u52a0\u6570\u5b57\u53ea\u4f1a\u4f7f\u603b\u548c\u66f4\u5927&#xff0c;\u5f53\u524d\u5206\u652f\u4e0d\u53ef\u80fd\u518d\u5f97\u5230\u6709\u6548\u7b54\u6848&#xff0c;\u56e0\u6b64\u53ef\u4ee5\u63d0\u524d\u526a\u679d\u3002<\/p>\n<p>\u4f8b\u5982\u76ee\u6807\u503c\u4e3a 9&#xff0c;\u5f53\u524d\u8def\u5f84\u4e3a [5,5]&#xff0c;\u603b\u548c\u5df2\u7ecf\u662f 10&#xff0c;\u5c31\u6ca1\u6709\u7ee7\u7eed\u641c\u7d22\u7684\u5fc5\u8981\u3002<\/p>\n<h3>\u4e94\u3001\u56de\u6eaf\u7684\u5b8c\u6574\u8fc7\u7a0b<\/h3>\n<p>\u5728\u6bcf\u4e00\u5c42\u9012\u5f52\u4e2d&#xff0c;\u9700\u8981\u5b8c\u6210\u201c\u9009\u62e9\u3001\u9012\u5f52\u3001\u64a4\u9500\u9009\u62e9\u201d\u4e09\u4e2a\u52a8\u4f5c&#xff1a;<\/p>\n<p>route.add(candidates[i]);<br \/>\nsum &#043;&#061; candidates[i];<\/p>\n<p>traverseTree(candidates, target, i);<\/p>\n<p>route.removeLast();<br \/>\nsum -&#061; candidates[i];<\/p>\n<p>\u5148\u628a\u5f53\u524d\u6570\u5b57\u52a0\u5165\u8def\u5f84\u5e76\u66f4\u65b0\u603b\u548c&#xff0c;\u7136\u540e\u8fdb\u5165\u4e0b\u4e00\u5c42\u7ee7\u7eed\u641c\u7d22\u3002\u9012\u5f52\u8fd4\u56de\u540e&#xff0c;\u5220\u9664\u521a\u521a\u52a0\u5165\u7684\u6570\u5b57&#xff0c;\u540c\u65f6\u6062\u590d sum&#xff0c;\u8ba9\u7a0b\u5e8f\u56de\u5230\u9009\u62e9\u8be5\u6570\u5b57\u4e4b\u524d\u7684\u72b6\u6001&#xff0c;\u518d\u5c1d\u8bd5\u540c\u4e00\u5c42\u7684\u5176\u4ed6\u5019\u9009\u6570\u5b57\u3002<\/p>\n<p>\u4f8b\u5982\u641c\u7d22 [3,3,3] \u5e76\u8bb0\u5f55\u7b54\u6848\u540e&#xff0c;\u9700\u8981\u4f9d\u6b21\u56de\u9000\u5230 [3,3]\u3001[3]\u3002\u53ea\u6709\u6062\u590d\u539f\u6765\u7684\u8def\u5f84\u548c\u603b\u548c&#xff0c;\u624d\u80fd\u7ee7\u7eed\u5c1d\u8bd5 [3,3,4]\u3001[3,4] \u7b49\u5176\u4ed6\u5206\u652f\u3002<\/p>\n<p>\u5982\u679c\u53ea\u5220\u9664\u8def\u5f84\u672b\u5c3e\u5143\u7d20&#xff0c;\u5374\u6ca1\u6709\u6062\u590d sum&#xff0c;\u8def\u5f84\u548c\u603b\u548c\u5c31\u4f1a\u4e0d\u4e00\u81f4&#xff0c;\u540e\u9762\u7684\u5224\u65ad\u4e5f\u4f1a\u5168\u90e8\u51fa\u9519\u3002<\/p>\n<h3>\u516d\u3001\u5b8c\u6574 Java \u4ee3\u7801<\/h3>\n<p>\u4e0b\u9762\u7684\u5b9e\u73b0\u4e0e\u4e0a\u8ff0\u601d\u8def\u4e00\u81f4&#xff1a;<\/p>\n<p>import java.util.ArrayList;<br \/>\nimport java.util.LinkedList;<br \/>\nimport java.util.List;<\/p>\n<p>class Solution {<br \/>\n    \/\/ \u4fdd\u5b58\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u7ec4\u5408<br \/>\n    private final List&lt;List&lt;Integer&gt;&gt; res &#061; new LinkedList&lt;&gt;();<\/p>\n<p>    \/\/ \u4fdd\u5b58\u5f53\u524d\u641c\u7d22\u8def\u5f84<br \/>\n    private final LinkedList&lt;Integer&gt; route &#061; new LinkedList&lt;&gt;();<\/p>\n<p>    \/\/ \u5f53\u524d\u8def\u5f84\u4e2d\u6240\u6709\u6570\u5b57\u4e4b\u548c<br \/>\n    private int sum &#061; 0;<\/p>\n<p>    public List&lt;List&lt;Integer&gt;&gt; combinationSum(int[] candidates, int target) {<br \/>\n        traverseTree(candidates, target, 0);<br \/>\n        return res;<br \/>\n    }<\/p>\n<p>    private void traverseTree(int[] candidates, int target, int start) {<br \/>\n        \/\/ \u5f53\u524d\u8def\u5f84\u6b63\u597d\u6ee1\u8db3\u8981\u6c42<br \/>\n        if (sum &#061;&#061; target) {<br \/>\n            res.add(new ArrayList&lt;&gt;(route));<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        \/\/ \u6570\u7ec4\u5143\u7d20\u5747\u4e3a\u6b63\u6570&#xff0c;\u8d85\u8fc7\u76ee\u6807\u503c\u540e\u65e0\u6cd5\u518d\u6062\u590d<br \/>\n        if (sum &gt; target) {<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        \/\/ \u53ea\u4ece start \u5f00\u59cb\u9009\u62e9&#xff0c;\u907f\u514d\u4ea7\u751f\u91cd\u590d\u6392\u5217<br \/>\n        for (int i &#061; start; i &lt; candidates.length; i&#043;&#043;) {<br \/>\n            \/\/ \u505a\u51fa\u9009\u62e9<br \/>\n            route.add(candidates[i]);<br \/>\n            sum &#043;&#061; candidates[i];<\/p>\n<p>            \/\/ \u4ecd\u4ece i \u5f00\u59cb&#xff0c;\u5141\u8bb8 candidates[i] \u88ab\u91cd\u590d\u9009\u62e9<br \/>\n            traverseTree(candidates, target, i);<\/p>\n<p>            \/\/ \u64a4\u9500\u9009\u62e9&#xff0c;\u6062\u590d\u8fdb\u5165\u9012\u5f52\u524d\u7684\u72b6\u6001<br \/>\n            route.removeLast();<br \/>\n            sum -&#061; candidates[i];<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u4e03\u3001\u793a\u4f8b\u6267\u884c\u8fc7\u7a0b<\/h3>\n<p>\u4ee5 candidates &#061; [2,3,6,7]\u3001target &#061; 7 \u4e3a\u4f8b\u3002<\/p>\n<p>\u641c\u7d22\u9996\u5148\u4ece 2 \u5f00\u59cb&#xff1a;<\/p>\n<p>[] \u2192 [2] \u2192 [2,2] \u2192 [2,2,2]<\/p>\n<p>\u5728 [2,2] \u7684\u57fa\u7840\u4e0a\u9009\u62e9 3&#xff0c;\u5f97\u5230 [2,2,3]&#xff0c;\u603b\u548c\u7b49\u4e8e 7&#xff0c;\u8bb0\u5f55\u7b54\u6848\u3002\u56de\u6eaf\u540e\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u6570\u5b57&#xff0c;\u8d85\u8fc7 7 \u7684\u5206\u652f\u4f1a\u76f4\u63a5\u8fd4\u56de\u3002<\/p>\n<p>\u5f53\u7b2c\u4e00\u5c42\u9009\u62e9 3 \u65f6&#xff0c;\u540e\u7eed\u53ea\u80fd\u7ee7\u7eed\u9009\u62e9 3\u30016\u30017&#xff0c;\u4e0d\u80fd\u518d\u56de\u5934\u9009\u62e9 2&#xff0c;\u56e0\u6b64\u4e0d\u4f1a\u751f\u6210 [3,2,2]\u3002\u6700\u540e\u7b2c\u4e00\u5c42\u9009\u62e9 7&#xff0c;\u5f97\u5230\u7b2c\u4e8c\u4e2a\u6709\u6548\u7ec4\u5408 [7]\u3002<\/p>\n<p>\u6700\u7ec8\u7ed3\u679c\u4e3a&#xff1a;<\/p>\n<p>[[2, 2, 3], [7]]<\/p>\n<h3>\u516b\u3001\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n<p>\u56de\u6eaf\u7b97\u6cd5\u9700\u8981\u679a\u4e3e\u53ef\u80fd\u7684\u7ec4\u5408&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u4e0e\u5019\u9009\u6570\u5b57\u53ca\u76ee\u6807\u503c\u6709\u5173\u3002\u82e5\u6700\u5c0f\u5019\u9009\u6570\u5b57\u4e3a m&#xff0c;\u51b3\u7b56\u6811\u6700\u5927\u6df1\u5ea6\u7ea6\u4e3a target \/ m&#xff0c;\u6700\u574f\u60c5\u51b5\u4e0b\u641c\u7d22\u89c4\u6a21\u5448\u6307\u6570\u7ea7\u589e\u957f&#xff0c;\u53ef\u7c97\u7565\u7406\u89e3\u4e3a O(n^(target\/m))\u3002<\/p>\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3b\u8981\u6765\u81ea\u9012\u5f52\u8c03\u7528\u6808\u548c\u5f53\u524d\u8def\u5f84&#xff0c;\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u7ea6\u4e3a target \/ m&#xff0c;\u56e0\u6b64\u989d\u5916\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(target\/m)&#xff0c;\u4e0d\u8ba1\u7b97\u6700\u7ec8\u7b54\u6848\u5360\u7528\u7684\u7a7a\u95f4\u3002<\/p>\n<h3>\u4e5d\u3001\u5e38\u89c1\u9519\u8bef<\/h3>\n<h4>1. \u6bcf\u5c42\u90fd\u4ece\u96f6\u5f00\u59cb\u904d\u5386<\/h4>\n<p>\u8fd9\u6837\u4f1a\u540c\u65f6\u4ea7\u751f [2,2,3] \u548c [3,2,2]&#xff0c;\u9020\u6210\u7ec4\u5408\u91cd\u590d\u3002\u5e94\u5f53\u4f7f\u7528 start \u63a7\u5236\u9009\u62e9\u8303\u56f4\u3002<\/p>\n<h4>2. \u9012\u5f52\u65f6\u4f20\u5165 i &#043; 1<\/h4>\n<p>\u8fd9\u4f1a\u5bfc\u81f4\u540c\u4e00\u4e2a\u6570\u5b57\u65e0\u6cd5\u91cd\u590d\u4f7f\u7528&#xff0c;\u6f0f\u6389 [2,2,3] \u8fd9\u6837\u7684\u7b54\u6848\u3002\u672c\u9898\u5e94\u5f53\u7ee7\u7eed\u4f20\u5165 i\u3002<\/p>\n<h4>3. \u76f4\u63a5\u628a route \u52a0\u5165\u7ed3\u679c\u96c6<\/h4>\n<p>route \u662f\u4e00\u4e2a\u4e0d\u65ad\u53d8\u5316\u7684\u5bf9\u8c61&#xff0c;\u5fc5\u987b\u4f7f\u7528 new ArrayList&lt;&gt;(route) \u4fdd\u5b58\u8def\u5f84\u5feb\u7167\u3002<\/p>\n<h4>4. \u9012\u5f52\u540e\u5fd8\u8bb0\u6062\u590d\u72b6\u6001<\/h4>\n<p>\u56de\u6eaf\u65f6\u65e2\u8981\u5220\u9664\u672b\u5c3e\u6570\u5b57&#xff0c;\u4e5f\u8981\u51cf\u53bb\u8be5\u6570\u5b57&#xff0c;\u4f7f route \u548c sum \u540c\u65f6\u6062\u590d\u3002<\/p>\n<h4>5. \u5ffd\u7565\u526a\u679d\u6210\u7acb\u7684\u524d\u63d0<\/h4>\n<p>sum &gt; target \u540e\u80fd\u591f\u76f4\u63a5\u8fd4\u56de&#xff0c;\u662f\u56e0\u4e3a\u9898\u76ee\u4e2d\u7684\u5019\u9009\u6570\u5b57\u90fd\u662f\u6b63\u6574\u6570\u3002\u5982\u679c\u5141\u8bb8\u8d1f\u6570&#xff0c;\u5c31\u4e0d\u80fd\u4f7f\u7528\u8fd9\u4e00\u526a\u679d\u903b\u8f91\u3002<\/p>\n<h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<p>\u7ec4\u5408\u603b\u548c\u662f\u4e00\u9053\u5178\u578b\u7684\u56de\u6eaf\u9898\u3002\u6211\u4eec\u901a\u8fc7 DFS \u679a\u4e3e\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9&#xff0c;\u4f7f\u7528 route \u7ef4\u62a4\u5f53\u524d\u7ec4\u5408&#xff0c;\u4f7f\u7528 sum \u5224\u65ad\u5f53\u524d\u72b6\u6001&#xff0c;\u5e76\u5728\u9012\u5f52\u8fd4\u56de\u540e\u64a4\u9500\u9009\u62e9\u3002<\/p>\n<p>\u672c\u9898\u6700\u5173\u952e\u7684\u662f start \u53c2\u6570&#xff1a;\u540c\u4e00\u5c42\u53ea\u4ece start \u4e4b\u540e\u9009\u62e9&#xff0c;\u53ef\u4ee5\u907f\u514d\u4e0d\u540c\u987a\u5e8f\u4ea7\u751f\u91cd\u590d\u7ec4\u5408&#xff1b;\u9012\u5f52\u65f6\u7ee7\u7eed\u4f20\u5165\u5f53\u524d\u4e0b\u6807 i&#xff0c;\u53c8\u80fd\u5141\u8bb8\u540c\u4e00\u4e2a\u6570\u5b57\u88ab\u91cd\u590d\u4f7f\u7528\u3002\u5f53\u603b\u548c\u8d85\u8fc7\u76ee\u6807\u503c\u65f6&#xff0c;\u518d\u5229\u7528\u6b63\u6574\u6570\u6761\u4ef6\u63d0\u524d\u526a\u679d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u9898\u76ee\u63cf\u8ff0\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u91cd\u590d\u5143\u7d20\u7684\u6b63\u6574\u6570\u6570\u7ec4 candidates \u548c\u4e00\u4e2a\u76ee\u6807\u6574\u6570 target&#xff0c;\u627e\u51fa\u6240\u6709\u6570\u5b57\u4e4b\u548c\u7b49\u4e8e target \u7684\u4e0d\u540c\u7ec4\u5408\u3002\u6570\u7ec4\u4e2d\u7684\u540c\u4e00\u4e2a\u6570\u5b57\u53ef\u4ee5\u88ab\u91cd\u590d\u9009\u62e9\u3002\u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u6570\u5b57\u7684\u9009\u62e9\u6b21\u6570\u4e0d\u540c&#xff0c;\u5c31\u8ba4\u4e3a\u662f\u4e0d\u540c\u7ec4\u5408\u3002\u9898\u76ee\u5141\u8bb8\u4ee5\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002\u4f8b\u5982&#xff1a;\u8f93\u5165&#xff1a;candidates  [2, 3, 6, 7], target  7<br \/>\n\u8f93\u51fa&#xff1a;[[2, 2, 3], [7]]\u6570\u5b57 2 \u53ef\u4ee5\u91cd\u590d\u4f7f\u7528&amp;#xf<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10187,99,2692,1983],"topic":[],"class_list":["post-93658","post","type-post","status-publish","format-standard","hentry","category-server","tag-codingplan","tag-java","tag-leetcode","tag-1983"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>LeetCode 39\uff1a\u7ec4\u5408\u603b\u548c\u2014\u2014Java DFS \u56de\u6eaf\u4e0e\u526a\u679d\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/93658.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LeetCode 39\uff1a\u7ec4\u5408\u603b\u548c\u2014\u2014Java DFS \u56de\u6eaf\u4e0e\u526a\u679d\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u9898\u76ee\u63cf\u8ff0\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u91cd\u590d\u5143\u7d20\u7684\u6b63\u6574\u6570\u6570\u7ec4 candidates \u548c\u4e00\u4e2a\u76ee\u6807\u6574\u6570 target&#xff0c;\u627e\u51fa\u6240\u6709\u6570\u5b57\u4e4b\u548c\u7b49\u4e8e target \u7684\u4e0d\u540c\u7ec4\u5408\u3002\u6570\u7ec4\u4e2d\u7684\u540c\u4e00\u4e2a\u6570\u5b57\u53ef\u4ee5\u88ab\u91cd\u590d\u9009\u62e9\u3002\u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u6570\u5b57\u7684\u9009\u62e9\u6b21\u6570\u4e0d\u540c&#xff0c;\u5c31\u8ba4\u4e3a\u662f\u4e0d\u540c\u7ec4\u5408\u3002\u9898\u76ee\u5141\u8bb8\u4ee5\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002\u4f8b\u5982&#xff1a;\u8f93\u5165&#xff1a;candidates [2, 3, 6, 7], target 7 \u8f93\u51fa&#xff1a;[[2, 2, 3], [7]]\u6570\u5b57 2 \u53ef\u4ee5\u91cd\u590d\u4f7f\u7528&amp;#xf\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/93658.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-12T15:24:54+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/93658.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/93658.html\",\"name\":\"LeetCode 39\uff1a\u7ec4\u5408\u603b\u548c\u2014\u2014Java DFS \u56de\u6eaf\u4e0e\u526a\u679d\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-12T15:24:54+00:00\",\"dateModified\":\"2026-08-12T15:24:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/93658.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/93658.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/93658.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LeetCode 39\uff1a\u7ec4\u5408\u603b\u548c\u2014\u2014Java DFS \u56de\u6eaf\u4e0e\u526a\u679d\u8be6\u89e3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LeetCode 39\uff1a\u7ec4\u5408\u603b\u548c\u2014\u2014Java DFS \u56de\u6eaf\u4e0e\u526a\u679d\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/93658.html","og_locale":"zh_CN","og_type":"article","og_title":"LeetCode 39\uff1a\u7ec4\u5408\u603b\u548c\u2014\u2014Java DFS \u56de\u6eaf\u4e0e\u526a\u679d\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u9898\u76ee\u63cf\u8ff0\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u91cd\u590d\u5143\u7d20\u7684\u6b63\u6574\u6570\u6570\u7ec4 candidates \u548c\u4e00\u4e2a\u76ee\u6807\u6574\u6570 target&#xff0c;\u627e\u51fa\u6240\u6709\u6570\u5b57\u4e4b\u548c\u7b49\u4e8e target \u7684\u4e0d\u540c\u7ec4\u5408\u3002\u6570\u7ec4\u4e2d\u7684\u540c\u4e00\u4e2a\u6570\u5b57\u53ef\u4ee5\u88ab\u91cd\u590d\u9009\u62e9\u3002\u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u6570\u5b57\u7684\u9009\u62e9\u6b21\u6570\u4e0d\u540c&#xff0c;\u5c31\u8ba4\u4e3a\u662f\u4e0d\u540c\u7ec4\u5408\u3002\u9898\u76ee\u5141\u8bb8\u4ee5\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002\u4f8b\u5982&#xff1a;\u8f93\u5165&#xff1a;candidates [2, 3, 6, 7], target 7 \u8f93\u51fa&#xff1a;[[2, 2, 3], [7]]\u6570\u5b57 2 \u53ef\u4ee5\u91cd\u590d\u4f7f\u7528&amp;#xf","og_url":"https:\/\/www.wsisp.com\/helps\/93658.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-12T15:24:54+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/93658.html","url":"https:\/\/www.wsisp.com\/helps\/93658.html","name":"LeetCode 39\uff1a\u7ec4\u5408\u603b\u548c\u2014\u2014Java DFS \u56de\u6eaf\u4e0e\u526a\u679d\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-12T15:24:54+00:00","dateModified":"2026-08-12T15:24:54+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/93658.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/93658.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/93658.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"LeetCode 39\uff1a\u7ec4\u5408\u603b\u548c\u2014\u2014Java DFS \u56de\u6eaf\u4e0e\u526a\u679d\u8be6\u89e3"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/93658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=93658"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/93658\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=93658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=93658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=93658"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=93658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}