{"id":5407,"date":"2021-07-17T18:14:59","date_gmt":"2021-07-17T10:14:59","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=5407"},"modified":"2021-07-19T16:42:36","modified_gmt":"2021-07-19T08:42:36","slug":"%e4%bc%98%e9%9b%85%e7%9a%84%e6%9e%9a%e4%b8%be%ef%bc%88%e5%9b%9e%e6%ba%af%ef%bc%89","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5407","title":{"rendered":"\u4f18\u96c5\u7684\u679a\u4e3e\uff08\u56de\u6eaf\uff09"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u6574\u6570 target \u3002<\/p>\n<p>\u5411\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6574\u6570\u524d\u6dfb\u52a0\u00a0&#8216;+&#8217; \u6216 &#8216;-&#8216; \uff0c\u7136\u540e\u4e32\u8054\u8d77\u6240\u6709\u6574\u6570\uff0c\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a \u8868\u8fbe\u5f0f \uff1a<\/p>\n<p>\u4f8b\u5982\uff0cnums = [2, 1] \uff0c\u53ef\u4ee5\u5728 2 \u4e4b\u524d\u6dfb\u52a0 &#8216;+&#8217; \uff0c\u5728 1 \u4e4b\u524d\u6dfb\u52a0 &#8216;-&#8216; \uff0c\u7136\u540e\u4e32\u8054\u8d77\u6765\u5f97\u5230\u8868\u8fbe\u5f0f &#8220;+2-1&#8221; \u3002<br \/>\n\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<\/p>\n<p>&nbsp;<\/p>\n<p>\u793a\u4f8b 1\uff1a<\/p>\n<p>\u8f93\u5165\uff1anums = [1,1,1,1,1], target = 3<br \/>\n\u8f93\u51fa\uff1a5<br \/>\n\u89e3\u91ca\uff1a\u4e00\u5171\u6709 5 \u79cd\u65b9\u6cd5\u8ba9\u6700\u7ec8\u76ee\u6807\u548c\u4e3a 3 \u3002<br \/>\n-1 + 1 + 1 + 1 + 1 = 3<br \/>\n+1 &#8211; 1 + 1 + 1 + 1 = 3<br \/>\n+1 + 1 &#8211; 1 + 1 + 1 = 3<br \/>\n+1 + 1 + 1 &#8211; 1 + 1 = 3<br \/>\n+1 + 1 + 1 + 1 &#8211; 1 = 3<br \/>\n\u793a\u4f8b 2\uff1a<\/p>\n<p>\u8f93\u5165\uff1anums = [1], target = 1<br \/>\n\u8f93\u51fa\uff1a1<br \/>\n\u63d0\u793a\uff1a<\/p>\n<p>1 &lt;= nums.length &lt;= 20<br \/>\n0 &lt;= nums[i] &lt;= 1000<br \/>\n0 &lt;= sum(nums[i]) &lt;= 1000<br \/>\n-1000 &lt;= target &lt;= 1000<br \/>\n\u76f8\u5173\u6807\u7b7e<br \/>\n\u6570\u7ec4<br \/>\n\u52a8\u6001\u89c4\u5212<br \/>\n\u56de\u6eaf<\/p>\n<p>C++<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f5c\u8005\uff1a\u529b\u6263 (LeetCode)<br \/>\n\u94fe\u63a5\uff1ahttps:\/\/leetcode-cn.com\/leetbook\/read\/queue-stack\/ga4o2\/<br \/>\n\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09<br \/>\n\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Solution {\r\npublic:\r\n    int findTargetSumWays(vector&lt;int&gt;&amp; nums, int target) {\r\n        if (nums.size() == 0)\r\n            return 0;\r\n\r\n        int result = 0;\r\n        backtrace(nums, target, result, 0, 0);\r\n        return result;\r\n    }\r\n\r\n    void backtrace(const vector&lt;int&gt;&amp; nums, int target, int&amp; result, int level, int sum) {\r\n        if (level == nums.size()) {\r\n            if (sum == target) {\r\n                result++;\r\n            }\r\n            return;\r\n        }\r\n\r\n        backtrace(nums, target, result, level + 1, sum + nums[level]);\r\n        backtrace(nums, target, result, level + 1, sum - nums[level]);\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u6574\u6570 <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[120],"class_list":["post-5407","post","type-post","status-publish","format-standard","hentry","tag-04-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=5407"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5407\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}