{"id":55380,"date":"2025-08-13T21:35:49","date_gmt":"2025-08-13T13:35:49","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/55380.html"},"modified":"2025-08-13T21:35:49","modified_gmt":"2025-08-13T13:35:49","slug":"%e3%80%90leetcode%e3%80%91%e9%9a%8f%e7%ac%94","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/55380.html","title":{"rendered":"\u3010Leetcode\u3011\u968f\u7b14"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\n<ul>\n<li>\u9898\u76ee\u4e00&#xff1a;\u76ee\u6807\u548c&#xff08;LeetCode 494&#xff09;<\/li>\n<li>\n<ul>\n<li>\u9898\u76ee\u5206\u6790&#xff1a;<\/li>\n<li>\u89e3\u9898\u601d\u8def&#xff1a;<\/li>\n<li>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/li>\n<li>\u4ee3\u7801\u89e3\u6790&#xff1a;<\/li>\n<\/ul>\n<\/li>\n<li>\u9898\u76ee\u4e8c&#xff1a;\u4e8c\u53c9\u6811\u7684\u5c42\u5e73\u5747\u503c&#xff08;LeetCode 637&#xff09;<\/li>\n<li>\n<ul>\n<li>\u9898\u76ee\u5206\u6790&#xff1a;<\/li>\n<li>\u89e3\u9898\u601d\u8def&#xff1a;<\/li>\n<li>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/li>\n<li>\u4ee3\u7801\u89e3\u6790&#xff1a;<\/li>\n<\/ul>\n<\/li>\n<li>\u9898\u76ee\u4e09&#xff1a;\u96f6\u94b1\u5151\u6362 II&#xff08;LeetCode 518&#xff09;<\/li>\n<li>\n<ul>\n<li>\u9898\u76ee\u5206\u6790&#xff1a;<\/li>\n<li>\u89e3\u9898\u601d\u8def&#xff1a;<\/li>\n<li>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/li>\n<li>\u4ee3\u7801\u89e3\u6790&#xff1a;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&#x1f308;\u4f60\u597d\u5440&#xff01;\u6211\u662f \u5c71\u9876\u98ce\u666f\u72ec\u597d &#x1f388;\u6b22\u8fce\u8e0f\u5165\u6211\u7684\u535a\u5ba2\u4e16\u754c&#xff0c;\u80fd\u4e0e\u60a8\u5728\u6b64\u9082\u9005&#xff0c;\u771f\u662f\u7f18\u5206\u4f7f\u7136&#xff01;&#x1f60a; &#x1f338;\u613f\u60a8\u5728\u6b64\u505c\u7559\u7684\u6bcf\u4e00\u523b&#xff0c;\u90fd\u6c90\u6d74\u5728\u8f7b\u677e\u6109\u60a6\u7684\u6c1b\u56f4\u4e2d\u3002 &#x1f4d6;\u8fd9\u91cc\u4e0d\u4ec5\u6709\u4e30\u5bcc\u7684\u77e5\u8bc6\u548c\u8da3\u5473\u6a2a\u751f\u7684\u5185\u5bb9\u7b49\u60a8\u6765\u63a2\u7d22&#xff0c;\u66f4\u662f\u4e00\u4e2a\u81ea\u7531\u4ea4\u6d41\u7684\u5e73\u53f0&#xff0c;\u671f\u5f85\u60a8\u7559\u4e0b\u72ec\u7279\u7684\u601d\u8003\u4e0e\u89c1\u89e3\u3002&#x1f31f; &#x1f680;\u8ba9\u6211\u4eec\u4e00\u8d77\u8e0f\u4e0a\u8fd9\u6bb5\u63a2\u7d22\u4e0e\u6210\u957f\u7684\u65c5\u7a0b&#xff0c;\u643a\u624b\u6316\u6398\u66f4\u591a\u53ef\u80fd&#xff0c;\u5171\u540c\u8fdb\u6b65&#xff01;&#x1f4aa;\u2728<\/p>\n<h3>\u9898\u76ee\u4e00&#xff1a;\u76ee\u6807\u548c&#xff08;LeetCode 494&#xff09;<\/h3>\n<h4>\u9898\u76ee\u5206\u6790&#xff1a;<\/h4>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u6574\u6570 target \u3002\u5411\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6574\u6570\u524d\u6dfb\u52a0 \u2018&#043;\u2019 \u6216 \u2018-\u2019 &#xff0c;\u7136\u540e\u4e32\u8054\u8d77\u6240\u6709\u6574\u6570&#xff0c;\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982&#xff0c;nums &#061; [2, 1] &#xff0c;\u53ef\u4ee5\u5728 2 \u524d\u6dfb\u52a0 \u2018&#043;\u2019 &#xff0c;\u5728 1 \u524d\u6dfb\u52a0 \u2018-\u2019 &#xff0c;\u5f97\u5230\u8868\u8fbe\u5f0f \u201c&#043;2-1\u201d \u3002\u8fd4\u56de\u53ef\u4ee5\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u6784\u9020\u7684\u3001\u8fd0\u7b97\u7ed3\u679c\u7b49\u4e8e target \u7684\u4e0d\u540c\u8868\u8fbe\u5f0f\u7684\u6570\u76ee\u3002\u4f8b\u5982&#xff1a;\u8f93\u5165nums &#061; [1,1,1,1,1], target &#061; 3&#xff0c;\u8f93\u51fa5\u3002<\/p>\n<h4>\u89e3\u9898\u601d\u8def&#xff1a;<\/h4>\n<p>\u95ee\u9898\u8f6c\u6362&#xff1a;\u8bbe\u6dfb\u52a0\u2019&#043;\u2018\u7684\u6570\u5b57\u548c\u4e3ax&#xff0c;\u6dfb\u52a0\u2019-&#039;\u7684\u6570\u5b57\u548c\u4e3ay&#xff0c;\u5219x &#8211; y &#061; target&#xff0c;\u4e14x &#043; y &#061; sum(nums)\u3002\u8054\u7acb\u5f97x &#061; (target &#043; sum(nums)) \/ 2&#xff0c;\u56e0\u6b64\u95ee\u9898\u8f6c\u5316\u4e3a\u6c42\u6570\u7ec4\u4e2d\u80fd\u51d1\u6210x\u7684\u5b50\u96c6\u6570\u91cf&#xff08;0-1\u80cc\u5305\u95ee\u9898&#xff09;\u3002 \u52a8\u6001\u89c4\u5212&#xff1a;\u5b9a\u4e49dp[i]\u4e3a\u80fd\u51d1\u6210\u548c\u4e3ai\u7684\u5b50\u96c6\u6570\u91cf&#xff0c;\u521d\u59cb\u5316dp[0] &#061; 1&#xff08;\u7a7a\u5b50\u96c6\u548c\u4e3a0&#xff09;\u3002 \u72b6\u6001\u8f6c\u79fb&#xff1a;\u5bf9\u4e8e\u6bcf\u4e2a\u6570\u5b57num&#xff0c;\u4ecex\u9006\u5e8f\u904d\u5386\u5230num&#xff0c;\u66f4\u65b0dp[i] &#043;&#061; dp[i &#8211; num]\u3002 \u8fb9\u754c\u68c0\u67e5&#xff1a;\u82e5sum(nums) &lt; target\u6216(target &#043; sum(nums))\u4e3a\u5947\u6570&#xff0c;\u5219\u65e0\u89e3&#xff0c;\u8fd4\u56de0\u3002<\/p>\n<h4>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/h4>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">findTargetSumWays<\/span><span class=\"token punctuation\">(<\/span>nums<span class=\"token punctuation\">,<\/span> target<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    total <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">sum<\/span><span class=\"token punctuation\">(<\/span>nums<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u8fb9\u754c\u6761\u4ef6&#xff1a;\u603b\u548c\u5c0f\u4e8e\u76ee\u6807\u503c\u6216\u65e0\u6cd5\u6574\u9664&#xff0c;\u65e0\u89e3<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> total <span class=\"token operator\">&lt;<\/span> <span class=\"token builtin\">abs<\/span><span class=\"token punctuation\">(<\/span>target<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">or<\/span> <span class=\"token punctuation\">(<\/span>total <span class=\"token operator\">&#043;<\/span> target<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">%<\/span> <span class=\"token number\">2<\/span> <span class=\"token operator\">!&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><br \/>\n    <span class=\"token comment\"># \u8ba1\u7b97\u9700\u8981\u51d1\u6210\u7684\u548cx<\/span><br \/>\n    x <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>total <span class=\"token operator\">&#043;<\/span> target<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">\/\/<\/span> <span class=\"token number\">2<\/span><br \/>\n    <span class=\"token comment\"># \u521d\u59cb\u5316dp\u6570\u7ec4&#xff0c;dp[i]\u8868\u793a\u51d1\u6210\u548c\u4e3ai\u7684\u5b50\u96c6\u6570\u91cf<\/span><br \/>\n    dp <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">*<\/span> <span class=\"token punctuation\">(<\/span>x <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    dp<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span>  <span class=\"token comment\"># \u7a7a\u5b50\u96c6\u548c\u4e3a0<\/span><\/p>\n<p>    <span class=\"token keyword\">for<\/span> num <span class=\"token keyword\">in<\/span> nums<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u9006\u5e8f\u904d\u5386&#xff0c;\u907f\u514d\u91cd\u590d\u4f7f\u7528\u5f53\u524d\u6570\u5b57<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> i <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">,<\/span> num <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            dp<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#043;&#061;<\/span> dp<span class=\"token punctuation\">[<\/span>i <span class=\"token operator\">&#8211;<\/span> num<span class=\"token punctuation\">]<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> dp<span class=\"token punctuation\">[<\/span>x<span class=\"token punctuation\">]<\/span><\/p>\n<p><span class=\"token comment\"># \u6d4b\u8bd5\u793a\u4f8b<\/span><br \/>\n<span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;__main__&#034;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    nums <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><br \/>\n    target <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">3<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u76ee\u6807\u548c\u7684\u8868\u8fbe\u5f0f\u6570\u76ee&#xff1a;&#034;<\/span><span class=\"token punctuation\">,<\/span> findTargetSumWays<span class=\"token punctuation\">(<\/span>nums<span class=\"token punctuation\">,<\/span> target<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8f93\u51fa&#xff1a;5<\/span><\/p>\n<h4>\u4ee3\u7801\u89e3\u6790&#xff1a;<\/h4>\n<p>\u901a\u8fc7\u6570\u5b66\u63a8\u5bfc\u5c06\u76ee\u6807\u548c\u95ee\u9898\u8f6c\u5316\u4e3a\u5b50\u96c6\u548c\u95ee\u9898&#xff0c;\u964d\u4f4e\u95ee\u9898\u590d\u6742\u5ea6\u3002 dp\u6570\u7ec4\u7684\u5927\u5c0f\u4e3ax &#043; 1&#xff0c;\u5176\u4e2dx\u662f\u9700\u8981\u51d1\u6210\u7684\u548c&#xff0c;dp[i]\u8bb0\u5f55\u51d1\u6210\u548ci\u7684\u65b9\u6cd5\u6570\u3002 \u904d\u5386\u6bcf\u4e2a\u6570\u5b57\u65f6&#xff0c;\u91c7\u7528\u9006\u5e8f\u66f4\u65b0dp\u6570\u7ec4&#xff08;\u4ecex\u5230num&#xff09;&#xff0c;\u786e\u4fdd\u6bcf\u4e2a\u6570\u5b57\u53ea\u88ab\u4f7f\u7528\u4e00\u6b21&#xff08;0-1\u80cc\u5305\u7279\u6027&#xff09;\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n\u00d7x)&#xff08;n\u4e3a\u6570\u7ec4\u957f\u5ea6&#xff0c;x\u4e3a\u76ee\u6807\u548c&#xff09;&#xff0c;\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(x)&#xff08;dp\u6570\u7ec4\u5927\u5c0f&#xff09;\u3002<\/p>\n<h3>\u9898\u76ee\u4e8c&#xff1a;\u4e8c\u53c9\u6811\u7684\u5c42\u5e73\u5747\u503c&#xff08;LeetCode 637&#xff09;<\/h3>\n<h4>\u9898\u76ee\u5206\u6790&#xff1a;<\/h4>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u975e\u7a7a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root , \u4ee5\u6570\u7ec4\u7684\u5f62\u5f0f\u8fd4\u56de\u6bcf\u4e00\u5c42\u8282\u70b9\u7684\u5e73\u5747\u503c\u3002\u4e0e\u5b9e\u9645\u7b54\u6848\u76f8\u5dee10\u207b\u2075\u4ee5\u5185\u7684\u7b54\u6848\u53ef\u4ee5\u88ab\u63a5\u53d7\u3002\u4f8b\u5982&#xff1a;\u8f93\u5165root &#061; [3,9,20,null,null,15,7]&#xff0c;\u8f93\u51fa[3.00000,14.50000,11.00000]\u3002<\/p>\n<h4>\u89e3\u9898\u601d\u8def&#xff1a;<\/h4>\n<p>\u5c42\u5e8f\u904d\u5386&#xff1a;\u5229\u7528\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22&#xff08;BFS&#xff09;\u904d\u5386\u6bcf\u4e00\u5c42\u8282\u70b9&#xff0c;\u8ba1\u7b97\u6bcf\u5c42\u7684\u5e73\u5747\u503c\u3002 \u961f\u5217\u8f85\u52a9&#xff1a;\u4f7f\u7528\u961f\u5217\u5b58\u50a8\u5f53\u524d\u5c42\u7684\u8282\u70b9&#xff0c;\u6bcf\u6b21\u904d\u5386\u961f\u5217\u4e2d\u6240\u6709\u8282\u70b9&#xff0c;\u8ba1\u7b97\u603b\u548c\u4e0e\u8282\u70b9\u6570&#xff0c;\u5f97\u5230\u5e73\u5747\u503c\u3002 \u904d\u5386\u8fc7\u7a0b&#xff1a;\u521d\u59cb\u5316\u961f\u5217\u5e76\u52a0\u5165\u6839\u8282\u70b9&#xff0c;\u5faa\u73af\u5904\u7406\u6bcf\u4e00\u5c42&#xff0c;\u8ba1\u7b97\u5e73\u5747\u503c\u540e\u5c06\u4e0b\u4e00\u5c42\u8282\u70b9&#xff08;\u5de6\u53f3\u5b50\u8282\u70b9&#xff09;\u52a0\u5165\u961f\u5217\u3002<\/p>\n<h4>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/h4>\n<p><span class=\"token keyword\">from<\/span> collections <span class=\"token keyword\">import<\/span> deque<\/p>\n<p><span class=\"token comment\"># \u4e8c\u53c9\u6811\u8282\u70b9\u5b9a\u4e49<\/span><br \/>\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">TreeNode<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> val<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> left<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">None<\/span><span class=\"token punctuation\">,<\/span> right<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">None<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>val <span class=\"token operator\">&#061;<\/span> val<br \/>\n        self<span class=\"token punctuation\">.<\/span>left <span class=\"token operator\">&#061;<\/span> left<br \/>\n        self<span class=\"token punctuation\">.<\/span>right <span class=\"token operator\">&#061;<\/span> right<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">averageOfLevels<\/span><span class=\"token punctuation\">(<\/span>root<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token keyword\">not<\/span> root<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><\/p>\n<p>    result <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><br \/>\n    q <span class=\"token operator\">&#061;<\/span> deque<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span>root<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u521d\u59cb\u5316\u961f\u5217&#xff0c;\u52a0\u5165\u6839\u8282\u70b9<\/span><\/p>\n<p>    <span class=\"token keyword\">while<\/span> q<span class=\"token punctuation\">:<\/span><br \/>\n        level_size <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">len<\/span><span class=\"token punctuation\">(<\/span>q<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5f53\u524d\u5c42\u7684\u8282\u70b9\u6570<\/span><br \/>\n        level_sum <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0.0<\/span>      <span class=\"token comment\"># \u5f53\u524d\u5c42\u7684\u603b\u548c<\/span><\/p>\n<p>        <span class=\"token comment\"># \u904d\u5386\u5f53\u524d\u5c42\u7684\u6240\u6709\u8282\u70b9<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> _ <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span>level_size<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            node <span class=\"token operator\">&#061;<\/span> q<span class=\"token punctuation\">.<\/span>popleft<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            level_sum <span class=\"token operator\">&#043;&#061;<\/span> node<span class=\"token punctuation\">.<\/span>val<\/p>\n<p>            <span class=\"token comment\"># \u52a0\u5165\u4e0b\u4e00\u5c42\u8282\u70b9<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> node<span class=\"token punctuation\">.<\/span>left<span class=\"token punctuation\">:<\/span><br \/>\n                q<span class=\"token punctuation\">.<\/span>append<span class=\"token punctuation\">(<\/span>node<span class=\"token punctuation\">.<\/span>left<span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> node<span class=\"token punctuation\">.<\/span>right<span class=\"token punctuation\">:<\/span><br \/>\n                q<span class=\"token punctuation\">.<\/span>append<span class=\"token punctuation\">(<\/span>node<span class=\"token punctuation\">.<\/span>right<span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\"># \u8ba1\u7b97\u5f53\u524d\u5c42\u7684\u5e73\u5747\u503c\u5e76\u52a0\u5165\u7ed3\u679c<\/span><br \/>\n        result<span class=\"token punctuation\">.<\/span>append<span class=\"token punctuation\">(<\/span>level_sum <span class=\"token operator\">\/<\/span> level_size<span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> result<\/p>\n<p><span class=\"token comment\"># \u8f85\u52a9\u51fd\u6570&#xff1a;\u6784\u5efa\u4e8c\u53c9\u6811&#xff08;\u540c\u524d&#xff09;<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">build_tree<\/span><span class=\"token punctuation\">(<\/span>values<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token keyword\">not<\/span> values<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">None<\/span><br \/>\n    root <span class=\"token operator\">&#061;<\/span> TreeNode<span class=\"token punctuation\">(<\/span>values<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    queue <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span>root<span class=\"token punctuation\">]<\/span><br \/>\n    i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> queue <span class=\"token keyword\">and<\/span> i <span class=\"token operator\">&lt;<\/span> <span class=\"token builtin\">len<\/span><span class=\"token punctuation\">(<\/span>values<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        node <span class=\"token operator\">&#061;<\/span> queue<span class=\"token punctuation\">.<\/span>pop<span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> values<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token keyword\">is<\/span> <span class=\"token keyword\">not<\/span> <span class=\"token boolean\">None<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            node<span class=\"token punctuation\">.<\/span>left <span class=\"token operator\">&#061;<\/span> TreeNode<span class=\"token punctuation\">(<\/span>values<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            queue<span class=\"token punctuation\">.<\/span>append<span class=\"token punctuation\">(<\/span>node<span class=\"token punctuation\">.<\/span>left<span class=\"token punctuation\">)<\/span><br \/>\n        i <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> i <span class=\"token operator\">&lt;<\/span> <span class=\"token builtin\">len<\/span><span class=\"token punctuation\">(<\/span>values<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">and<\/span> values<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token keyword\">is<\/span> <span class=\"token keyword\">not<\/span> <span class=\"token boolean\">None<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            node<span class=\"token punctuation\">.<\/span>right <span class=\"token operator\">&#061;<\/span> TreeNode<span class=\"token punctuation\">(<\/span>values<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            queue<span class=\"token punctuation\">.<\/span>append<span class=\"token punctuation\">(<\/span>node<span class=\"token punctuation\">.<\/span>right<span class=\"token punctuation\">)<\/span><br \/>\n        i <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> root<\/p>\n<p><span class=\"token comment\"># \u6d4b\u8bd5\u793a\u4f8b<\/span><br \/>\n<span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;__main__&#034;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u6784\u5efa\u793a\u4f8b\u6811&#xff1a;[3,9,20,null,null,15,7]<\/span><br \/>\n    values <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">9<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">20<\/span><span class=\"token punctuation\">,<\/span><span class=\"token boolean\">None<\/span><span class=\"token punctuation\">,<\/span><span class=\"token boolean\">None<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">15<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">7<\/span><span class=\"token punctuation\">]<\/span><br \/>\n    root <span class=\"token operator\">&#061;<\/span> build_tree<span class=\"token punctuation\">(<\/span>values<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6bcf\u5c42\u8282\u70b9\u7684\u5e73\u5747\u503c&#xff1a;&#034;<\/span><span class=\"token punctuation\">,<\/span> averageOfLevels<span class=\"token punctuation\">(<\/span>root<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8f93\u51fa&#xff1a;[3.0, 14.5, 11.0]<\/span><\/p>\n<h4>\u4ee3\u7801\u89e3\u6790&#xff1a;<\/h4>\n<p>\u901a\u8fc7\u5c42\u5e8f\u904d\u5386&#xff08;BFS&#xff09;\u5b9e\u73b0&#xff0c;\u961f\u5217\u5b58\u50a8\u6bcf\u4e00\u5c42\u7684\u8282\u70b9&#xff0c;level_size\u8bb0\u5f55\u5f53\u524d\u5c42\u8282\u70b9\u6570\u91cf\u3002 \u904d\u5386\u5f53\u524d\u5c42\u8282\u70b9\u65f6&#xff0c;\u7d2f\u52a0\u8282\u70b9\u503c\u5f97\u5230level_sum&#xff0c;\u8ba1\u7b97\u5e73\u5747\u503c\u540e\u52a0\u5165\u7ed3\u679c\u5217\u8868\u3002 \u4e0b\u4e00\u5c42\u8282\u70b9\u6309\u5de6\u53f3\u987a\u5e8f\u52a0\u5165\u961f\u5217&#xff0c;\u786e\u4fdd\u5c42\u5e8f\u904d\u5386\u7684\u6b63\u786e\u6027\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)&#xff08;n\u4e3a\u8282\u70b9\u603b\u6570&#xff0c;\u6bcf\u4e2a\u8282\u70b9\u8bbf\u95ee\u4e00\u6b21&#xff09;&#xff0c;\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(n)&#xff08;\u961f\u5217\u6700\u591a\u5b58\u50a8\u4e00\u5c42\u8282\u70b9&#xff09;\u3002<\/p>\n<h3>\u9898\u76ee\u4e09&#xff1a;\u96f6\u94b1\u5151\u6362 II&#xff08;LeetCode 518&#xff09;<\/h3>\n<h4>\u9898\u76ee\u5206\u6790&#xff1a;<\/h4>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 coins \u8868\u793a\u4e0d\u540c\u9762\u989d\u7684\u786c\u5e01&#xff0c;\u53e6\u7ed9\u4e00\u4e2a\u6574\u6570 amount \u8868\u793a\u603b\u91d1\u989d\u3002\u8ba1\u7b97\u5e76\u8fd4\u56de\u53ef\u4ee5\u51d1\u6210\u603b\u91d1\u989d\u7684\u786c\u5e01\u7ec4\u5408\u6570\u3002\u5982\u679c\u4efb\u4f55\u786c\u5e01\u7ec4\u5408\u90fd\u65e0\u6cd5\u51d1\u51fa\u603b\u91d1\u989d&#xff0c;\u8fd4\u56de 0 \u3002\u5047\u8bbe\u6bcf\u4e00\u79cd\u9762\u989d\u7684\u786c\u5e01\u6709\u65e0\u9650\u4e2a\u3002\u4f8b\u5982&#xff1a;\u8f93\u5165coins &#061; [1,2,5], amount &#061; 5&#xff0c;\u8f93\u51fa4&#xff08;\u7ec4\u5408\u4e3a&#xff1a;[1,1,1,1,1]\u3001[1,1,3]\u65e0\u6548\u2192\u6b63\u786e\u4e3a[1,1,1,2]\u3001[1,2,2]\u3001[5]&#xff09;\u3002<\/p>\n<h4>\u89e3\u9898\u601d\u8def&#xff1a;<\/h4>\n<p>\u52a8\u6001\u89c4\u5212&#xff1a;\u5b9a\u4e49dp[i]\u4e3a\u51d1\u6210\u91d1\u989di\u7684\u7ec4\u5408\u6570&#xff0c;\u521d\u59cb\u5316dp[0] &#061; 1&#xff08;\u7a7a\u7ec4\u5408&#xff09;\u3002 \u72b6\u6001\u8f6c\u79fb&#xff1a;\u5bf9\u4e8e\u6bcf\u4e2a\u786c\u5e01\u9762\u989dcoin&#xff0c;\u4ececoin\u5230amount\u6b63\u5e8f\u904d\u5386&#xff0c;\u66f4\u65b0dp[i] &#043;&#061; dp[i &#8211; coin]&#xff08;\u7d2f\u52a0\u4f7f\u7528\u5f53\u524d\u786c\u5e01\u7684\u7ec4\u5408\u6570&#xff09;\u3002 \u904d\u5386\u987a\u5e8f&#xff1a;\u5148\u904d\u5386\u786c\u5e01&#xff0c;\u518d\u904d\u5386\u91d1\u989d&#xff0c;\u786e\u4fdd\u6bcf\u4e2a\u7ec4\u5408\u7684\u786c\u5e01\u987a\u5e8f\u552f\u4e00&#xff08;\u907f\u514d\u91cd\u590d\u8ba1\u6570&#xff0c;\u5982[1,2]\u548c[2,1]\u89c6\u4e3a\u540c\u4e00\u7ec4\u5408&#xff09;\u3002<\/p>\n<h4>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/h4>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">change<\/span><span class=\"token punctuation\">(<\/span>amount<span class=\"token punctuation\">,<\/span> coins<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u521d\u59cb\u5316dp\u6570\u7ec4&#xff0c;dp[i]\u8868\u793a\u51d1\u6210\u91d1\u989di\u7684\u7ec4\u5408\u6570<\/span><br \/>\n    dp <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">*<\/span> <span class=\"token punctuation\">(<\/span>amount <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    dp<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span>  <span class=\"token comment\"># \u57fa\u7840\u60c5\u51b5&#xff1a;\u51d10\u5143\u67091\u79cd\u65b9\u6cd5&#xff08;\u4e0d\u9009\u4efb\u4f55\u786c\u5e01&#xff09;<\/span><\/p>\n<p>    <span class=\"token comment\"># \u904d\u5386\u6bcf\u79cd\u786c\u5e01<\/span><br \/>\n    <span class=\"token keyword\">for<\/span> coin <span class=\"token keyword\">in<\/span> coins<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u6b63\u5e8f\u904d\u5386\u91d1\u989d&#xff0c;\u786e\u4fdd\u6bcf\u4e2a\u786c\u5e01\u53ef\u4ee5\u88ab\u591a\u6b21\u4f7f\u7528<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> i <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span>coin<span class=\"token punctuation\">,<\/span> amount <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            dp<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#043;&#061;<\/span> dp<span class=\"token punctuation\">[<\/span>i <span class=\"token operator\">&#8211;<\/span> coin<span class=\"token punctuation\">]<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> dp<span class=\"token punctuation\">[<\/span>amount<span class=\"token punctuation\">]<\/span><\/p>\n<p><span class=\"token comment\"># \u6d4b\u8bd5\u793a\u4f8b<\/span><br \/>\n<span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;__main__&#034;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    coins <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">5<\/span><span class=\"token punctuation\">]<\/span><br \/>\n    amount <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">5<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u51d1\u6210\u91d1\u989d{}\u7684\u7ec4\u5408\u6570&#xff1a;&#034;<\/span><span class=\"token punctuation\">.<\/span><span class=\"token builtin\">format<\/span><span class=\"token punctuation\">(<\/span>amount<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> change<span class=\"token punctuation\">(<\/span>amount<span class=\"token punctuation\">,<\/span> coins<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8f93\u51fa&#xff1a;4<\/span><\/p>\n<h4>\u4ee3\u7801\u89e3\u6790&#xff1a;<\/h4>\n<p>dp\u6570\u7ec4\u7684\u5927\u5c0f\u4e3aamount &#043; 1&#xff0c;dp[0]\u521d\u59cb\u5316\u4e3a1\u8868\u793a\u51d10\u5143\u53ea\u67091\u79cd\u65b9\u6cd5&#xff08;\u7a7a\u7ec4\u5408&#xff09;\u3002 \u5916\u5c42\u5faa\u73af\u904d\u5386\u6bcf\u79cd\u786c\u5e01&#xff0c;\u5185\u5c42\u5faa\u73af\u4ece\u786c\u5e01\u9762\u989d\u5f00\u59cb\u6b63\u5e8f\u904d\u5386\u91d1\u989d&#xff0c;\u786e\u4fdd\u6bcf\u4e2a\u786c\u5e01\u53ef\u4ee5\u88ab\u91cd\u590d\u4f7f\u7528&#xff08;\u5b8c\u5168\u80cc\u5305\u7279\u6027&#xff09;\u3002 dp[i]\u7684\u66f4\u65b0\u903b\u8f91\u4e3a\u7d2f\u52a0\u4f7f\u7528\u5f53\u524d\u786c\u5e01\u7684\u7ec4\u5408\u6570&#xff08;\u5373dp[i &#8211; coin]&#xff09;&#xff0c;\u6700\u7ec8dp[amount]\u5373\u4e3a\u6240\u6c42\u7ec4\u5408\u6570\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(amount\u00d7k)&#xff08;k\u4e3a\u786c\u5e01\u79cd\u7c7b\u6570&#xff09;&#xff0c;\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(amount)&#xff08;dp\u6570\u7ec4\u5927\u5c0f&#xff09;\u3002<\/p>\n<hr \/>\n<p>\u2728 \u8fd9\u5c31\u662f\u4eca\u5929\u8981\u5206\u4eab\u7ed9\u5927\u5bb6\u7684\u5168\u90e8\u5185\u5bb9\u4e86&#xff0c;\u6211\u4eec\u4e0b\u671f\u518d\u89c1&#xff01;&#x1f60a; &#x1f3e0; \u6211\u5728CSDN\u7b49\u4f60\u54e6&#xff01;\u6211\u7684\u4e3b\u9875&#x1f60d;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb285\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u4f8b\u5982\uff0cnums = [2, 1] \uff0c\u53ef\u4ee5\u5728 2 \u524d\u6dfb\u52a0 \u2018+\u2019 \uff0c\u5728 1 \u524d\u6dfb\u52a0 \u2018-\u2019 \uff0c\u5f97\u5230\u8868\u8fbe\u5f0f \u201c+2-1\u201d\u3002\u4f8b\u5982\uff1a\u8f93\u5165nums = [1,1,1,1,1], target = 3\uff0c\u8f93\u51fa5\u3002\u4f8b\u5982\uff1a\u8f93\u5165coins = [1,2,5], amount = 5\uff0c\u8f93\u51fa4\uff08\u7ec4\u5408\u4e3a\uff1a[1,1,1,1,1]\u3001[1,1,3]\u65e0\u6548\u2192\u6b63\u786e\u4e3a[1,1,1,2]\u3001[1,2,2]\u3001[5]\uff09\u3002\u904d\u5386\u987a\u5e8f\uff1a\u5148\u904d\u5386\u786c\u5e01\uff0c\u518d\u904d\u5386\u91d1\u989d\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7ec4\u5408\u7684\u786c\u5e01\u987a\u5e8f\u552f\u4e00\uff08\u907f\u514d\u91cd\u590d\u8ba1\u6570\uff0c\u5982[1,2]\u548c[2,1]\u89c6\u4e3a\u540c\u4e00\u7ec4\u5408\uff09\u3002<\/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":[2692,5694,1813,427],"topic":[],"class_list":{"0":"post-55380","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-server","7":"tag-leetcode","9":"tag-1813","10":"tag-427"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Leetcode\u3011\u968f\u7b14 - \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\/55380.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Leetcode\u3011\u968f\u7b14 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb285\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u4f8b\u5982\uff0cnums = [2, 1] \uff0c\u53ef\u4ee5\u5728 2 \u524d\u6dfb\u52a0 \u2018+\u2019 \uff0c\u5728 1 \u524d\u6dfb\u52a0 \u2018-\u2019 \uff0c\u5f97\u5230\u8868\u8fbe\u5f0f \u201c+2-1\u201d\u3002\u4f8b\u5982\uff1a\u8f93\u5165nums = [1,1,1,1,1], target = 3\uff0c\u8f93\u51fa5\u3002\u4f8b\u5982\uff1a\u8f93\u5165coins = [1,2,5], amount = 5\uff0c\u8f93\u51fa4\uff08\u7ec4\u5408\u4e3a\uff1a[1,1,1,1,1]\u3001[1,1,3]\u65e0\u6548\u2192\u6b63\u786e\u4e3a[1,1,1,2]\u3001[1,2,2]\u3001[5]\uff09\u3002\u904d\u5386\u987a\u5e8f\uff1a\u5148\u904d\u5386\u786c\u5e01\uff0c\u518d\u904d\u5386\u91d1\u989d\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7ec4\u5408\u7684\u786c\u5e01\u987a\u5e8f\u552f\u4e00\uff08\u907f\u514d\u91cd\u590d\u8ba1\u6570\uff0c\u5982[1,2]\u548c[2,1]\u89c6\u4e3a\u540c\u4e00\u7ec4\u5408\uff09\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/55380.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-13T13:35:49+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=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/55380.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/55380.html\",\"name\":\"\u3010Leetcode\u3011\u968f\u7b14 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-13T13:35:49+00:00\",\"dateModified\":\"2025-08-13T13:35:49+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/55380.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/55380.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/55380.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Leetcode\u3011\u968f\u7b14\"}]},{\"@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":"\u3010Leetcode\u3011\u968f\u7b14 - \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\/55380.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Leetcode\u3011\u968f\u7b14 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb285\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u4f8b\u5982\uff0cnums = [2, 1] \uff0c\u53ef\u4ee5\u5728 2 \u524d\u6dfb\u52a0 \u2018+\u2019 \uff0c\u5728 1 \u524d\u6dfb\u52a0 \u2018-\u2019 \uff0c\u5f97\u5230\u8868\u8fbe\u5f0f \u201c+2-1\u201d\u3002\u4f8b\u5982\uff1a\u8f93\u5165nums = [1,1,1,1,1], target = 3\uff0c\u8f93\u51fa5\u3002\u4f8b\u5982\uff1a\u8f93\u5165coins = [1,2,5], amount = 5\uff0c\u8f93\u51fa4\uff08\u7ec4\u5408\u4e3a\uff1a[1,1,1,1,1]\u3001[1,1,3]\u65e0\u6548\u2192\u6b63\u786e\u4e3a[1,1,1,2]\u3001[1,2,2]\u3001[5]\uff09\u3002\u904d\u5386\u987a\u5e8f\uff1a\u5148\u904d\u5386\u786c\u5e01\uff0c\u518d\u904d\u5386\u91d1\u989d\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7ec4\u5408\u7684\u786c\u5e01\u987a\u5e8f\u552f\u4e00\uff08\u907f\u514d\u91cd\u590d\u8ba1\u6570\uff0c\u5982[1,2]\u548c[2,1]\u89c6\u4e3a\u540c\u4e00\u7ec4\u5408\uff09\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/55380.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-13T13:35:49+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/55380.html","url":"https:\/\/www.wsisp.com\/helps\/55380.html","name":"\u3010Leetcode\u3011\u968f\u7b14 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-13T13:35:49+00:00","dateModified":"2025-08-13T13:35:49+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/55380.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/55380.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/55380.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Leetcode\u3011\u968f\u7b14"}]},{"@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\/55380","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=55380"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/55380\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=55380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=55380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=55380"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=55380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}