{"id":126,"date":"2024-11-26T22:44:24","date_gmt":"2024-11-26T14:44:24","guid":{"rendered":"http:\/\/getzs.online\/?p=117"},"modified":"2024-11-28T08:53:24","modified_gmt":"2024-11-28T00:53:24","slug":"leetcode-hot100-1","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2024\/11\/26\/leetcode-hot100-1\/","title":{"rendered":"LeetCode Hot100 -1"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/two-sum\/description\/?envType=study-plan-v2&amp;envId=top-100-liked\">\u4e24\u6570\u4e4b\u548c<\/a><\/p>\n\n\n\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\uff0c\u5e76\u4e14\u4f60\u4e0d\u80fd\u4f7f\u7528\u4e24\u6b21\u76f8\u540c\u7684\u5143\u7d20\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<br><strong>\u8f93\u51fa\uff1a<\/strong>[0,1]<br><strong>\u89e3\u91ca\uff1a<\/strong>\u56e0\u4e3a nums[0] + nums[1] == 9 \uff0c\u8fd4\u56de [0, 1] \u3002<\/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<br><strong>\u8f93\u51fa\uff1a<\/strong>[1,2]<\/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<br><strong>\u8f93\u51fa\uff1a<\/strong>[0,1]<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\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<h2 class=\"wp-block-heading\">\u5e38\u89c4\u505a\u6cd5\uff1a<\/h2>\n\n\n\n<p>\u901a\u8fc7\u679a\u4e3e\uff0c\u904d\u5386\u6240\u6709\u79cd\u53ef\u80fd\u6027\uff0c\u590d\u6742\u5ea6\u4e3aO(n<sup>2<\/sup>).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tpublic int&#91;] twoSum(int&#91;] nums, int target) {\n\t\tint n = nums.length;\n\t\tfor (int i = 0 ; i &lt; n ; i++) {\n\t\t\tfor (int j = i + 1 ; j &lt; n ; j++) {\n\t\t\t\tif (nums&#91;i] + nums&#91;j] == target) {\n\t\t\t\t\treturn new int&#91;]{i, j};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn null;\n\t}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u54c8\u5e0c\u8868\u505a\u6cd5\uff1a<\/h2>\n\n\n\n<p>\u4f7f\u7528hash\u8868\uff0c\u7528\u7a7a\u95f4\u6362\u65f6\u95f4\uff0c\u590d\u6742\u5ea6O(n).<\/p>\n\n\n\n<p>\u901a\u8fc7hash\u8868\u8bb0\u5f55\u4e4b\u524d\u51fa\u73b0\u8fc7\u7684\u6570\u5b57\uff0c\u5224\u65ad\u679a\u4e3e\u7684num\u503c\u76f8\u52a0\u662f\u5426\u53ef\u4ee5\u6ee1\u8db3\u7b49\u4e8etarget\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tpublic int&#91;] twoSum(int&#91;] nums, int target) {\n\t\tMap&lt;Integer, Integer> hash = new HashMap&lt;>();\n\t\tfor (int i = 0 ; i &lt; nums.length ; i++) {\n\t\t\tint difference = target - nums&#91;i];\n\t\t\tif (hash.get(difference) != null) {\n\t\t\t\treturn new int&#91;]{hash.get(difference), i};\n\t\t\t}\n\t\t\thash.put(nums&#91;i], i);\n\t\t}\n\t\treturn null;\n\t}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8fdb\u9636\u505a\u6cd5\uff1a<\/h2>\n\n\n\n<p>        \u5229\u7528\u6982\u7387\u6027\uff0c\u4ece\u4e24\u8fb9\u5f80\u4e2d\u95f4\u540c\u65f6\u627e \u6bd4 \u540c\u4e00\u4fa7\u627e\u5355\u72ec\u627e\u5339\u914d\u7684\u6982\u7387\u66f4\u9ad8\uff0c\u56e0\u4e3a\u7ed3\u679c\u4e3a\u968f\u673a\u5206\u5e03\uff0c\u540c\u65f6\u6bd4\u5355\u72ec\u627e\u6982\u7387\u9ad8\u4e24\u500d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tpublic int&#91;] twoSum(int&#91;] nums, int target) {\n\t\tMap&lt;Integer, Integer> hash = new HashMap&lt;>();\n\t\tint n = nums.length;\n\t\tfor (int i = 0, j = n - 1 ; i &lt;= j ; i++, j--) {\n\t\t\tint difference = target - nums&#91;i];\n\t\t\tif (nums&#91;i] + nums&#91;j] == target &amp;&amp; i != j) {\n\t\t\t\treturn new int&#91;]{i, j};\n\t\t\t}\n\t\t\tif (hash.get(difference) != null) {\n\t\t\t\treturn new int&#91;]{hash.get(difference), i};\n\t\t\t}\n\t\t\tdifference = target - nums&#91;j];\n\t\t\tif (hash.get(difference) != null) {\n\t\t\t\treturn new int&#91;]{hash.get(difference), j};\n\t\t\t}\n\t\t\thash.put(nums&#91;i], i);\n\t\t\thash.put(nums&#91;j], j);\n\t\t}\n\t\treturn null;\n\t}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e24\u6570\u4e4b\u548c \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;nums&nbsp;\u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c&nbsp;target\uff0c\u8bf7\u4f60\u5728\u8be5\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[3],"class_list":["post-126","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-leetcode-hot100"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions\/181"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}