{"id":1897,"date":"2023-09-24T17:02:27","date_gmt":"2023-09-24T09:02:27","guid":{"rendered":"https:\/\/white-night.club\/?p=1897"},"modified":"2023-11-09T11:07:52","modified_gmt":"2023-11-09T03:07:52","slug":"ad14","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/09\/24\/ad14\/","title":{"rendered":"1.\u4e24\u6570\u4e4b\u548c"},"content":{"rendered":"\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c&nbsp;<code>target<\/code>\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa&nbsp;<strong>\u548c\u4e3a\u76ee\u6807\u503c&nbsp;<\/strong><em><code>target<\/code><\/em>&nbsp; \u7684\u90a3&nbsp;<strong>\u4e24\u4e2a<\/strong>&nbsp;\u6574\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u4e0b\u6807\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5047\u8bbe\u6bcf\u79cd\u8f93\u5165\u53ea\u4f1a\u5bf9\u5e94\u4e00\u4e2a\u7b54\u6848\u3002\u4f46\u662f\uff0c\u6570\u7ec4\u4e2d\u540c\u4e00\u4e2a\u5143\u7d20\u5728\u7b54\u6848\u91cc\u4e0d\u80fd\u91cd\u590d\u51fa\u73b0\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [2,7,11,15], target = 9\n<strong>\u8f93\u51fa\uff1a<\/strong>[0,1]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u56e0\u4e3a nums[0] + nums[1] == 9 \uff0c\u8fd4\u56de [0, 1] \u3002\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [3,2,4], target = 6\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,2]\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [3,3], target = 6\n<strong>\u8f93\u51fa\uff1a<\/strong>[0,1]\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li><code>2 &lt;= nums.length &lt;= 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>-10<sup>9<\/sup> &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li>\n\n\n\n<li><code>-10<sup>9<\/sup> &lt;= target &lt;= 10<sup>9<\/sup><\/code><\/li>\n\n\n\n<li><strong>\u53ea\u4f1a\u5b58\u5728\u4e00\u4e2a\u6709\u6548\u7b54\u6848<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>\u8fdb\u9636\uff1a<\/strong>\u4f60\u53ef\u4ee5\u60f3\u51fa\u4e00\u4e2a\u65f6\u95f4\u590d\u6742\u5ea6\u5c0f\u4e8e&nbsp;<code>O(n<sup>2<\/sup>)<\/code>&nbsp;\u7684\u7b97\u6cd5\u5417\uff1f<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u53d7\u4e0d\u4e86\u4e86\uff0c\u53ccfor\u628a\u54c8\u5e0c\u8868\u6253\u7206\u3002<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u770b\u4e86leetcode\u5b98\u65b9\u7684\u9898\u89e3\u7528\u5230\u4e86\u54c8\u5e0c\u8868\u3002\u4f46\u662f\u6211\u5b8c\u5168\u6ca1\u60f3\u5230\u8981\u7528HashMap..\u6240\u4ee5\u66b4\u529b\u8df3\u8fc7\u3002<\/p>\n\n\n\n<p>\u66b4\u529b\u7684\u8bdd\u6ca1\u5565\u8bf4\u7684\u4e86\uff0c\u76f4\u63a5\u4e24\u4e2afor\u4e00\u4e2a\u4e00\u4e2a\u67e5\u5c31\u884c\u4e86\u3002\u770b\u5230leetcode\u8bc4\u8bba\u533a\u8bf4\u53ef\u4ee5\u4e8c\u7ef4\u6570\u7ec4\uff08\u5b58\u7d22\u5f15\uff09+\u4e8c\u5206\u67e5\u627e\u89e3\u51b3\uff0c\u6709\u65f6\u95f4\u8bd5\u8bd5\u3002<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u5b8c\u6574\u4ee3\u7801 \uff08C++\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    vector&lt;int&gt; twoSum(vector&lt;int&gt;&amp; nums, int target) {\n        int len=nums.size();\n        for(int i=0;i&lt;len-1;i++)\n            for(int j=i+1;j&lt;len;j++)\n                if(nums&#91;i]+nums&#91;j]==target)\n                {\n                    return {i,j};\n                }\n        return {};\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u540e\u9762\u590d\u76d8\u7684\u65f6\u5019\u7528JavaScript\u5199\u4e86\u4e2a\u7528\u54c8\u5e0c\u8868\u7684\uff0c\u611f\u89c9\u54c8\u5e0c\u8868\u5176\u5b9e\u6bd4\u66b4\u529b\u66f4\u76f4\u89c2\uff1f\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * @param {number&#91;]} nums\n * @param {number} target\n * @return {number&#91;]}\n *\/\nvar twoSum = function(nums, target) {\n    const numMap = new Map();\n    \n    for (let i = 0; i &lt; nums.length; i++) {\n        const complement = target - nums&#91;i];\n        if (numMap.has(complement)) {\n            return &#91;numMap.get(complement), i];\n        }\n        numMap.set(nums&#91;i], i);\n    }\n    return &#91;];\n};\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1a\u66b4\u529b\uff08C++\uff09&#038;&#038;\u54c8\u5e0c\u8868\uff08JavaScript\uff09<\/p>\n","protected":false},"author":1,"featured_media":2827,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1897"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=1897"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1897\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2827"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=1897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=1897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=1897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}