{"id":97,"date":"2023-12-31T03:43:51","date_gmt":"2023-12-30T19:43:51","guid":{"rendered":"https:\/\/www.qicy.top\/?p=97"},"modified":"2023-12-31T04:09:57","modified_gmt":"2023-12-30T20:09:57","slug":"leetcode1-liangshuzhihe","status":"publish","type":"post","link":"https:\/\/www.qicy.top\/?p=97","title":{"rendered":"Leetcode\uff1a1.\u4e24\u6570\u4e4b\u548c"},"content":{"rendered":"<h2><a id=\"%E9%A2%98%E7%9B%AE\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u9898\u76ee<\/h2>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>nums<\/code> \u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c <code>target<\/code>\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa \u548c\u4e3a\u76ee\u6807\u503c <code>target<\/code>  \u7684\u90a3 \u4e24\u4e2a \u6574\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u4e0b\u6807\u3002<\/p>\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<p>\u4f60\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002<\/p>\n<p>\u793a\u4f8b 1\uff1a<\/p>\n<blockquote>\n<p>\u8f93\u5165\uff1anums = [2,7,11,15], target = 9<br \/>\n\u8f93\u51fa\uff1a[0,1]<br \/>\n\u89e3\u91ca\uff1a\u56e0\u4e3a nums[0] + nums[1] == 9 \uff0c\u8fd4\u56de [0, 1] \u3002<\/p>\n<\/blockquote>\n<p>\u793a\u4f8b 2\uff1a<\/p>\n<blockquote>\n<p>\u8f93\u5165\uff1anums = [3,2,4], target = 6<br \/>\n\u8f93\u51fa\uff1a[1,2]<\/p>\n<\/blockquote>\n<p>\u793a\u4f8b 3\uff1a<\/p>\n<blockquote>\n<p>\u8f93\u5165\uff1anums = [3,3], target = 6<br \/>\n\u8f93\u51fa\uff1a[0,1]<\/p>\n<\/blockquote>\n<p>\u63d0\u793a\uff1a<\/p>\n<ul>\n<li><code>2 &lt;= nums.length &lt;= 10^4<\/code><\/li>\n<li><code>-10^9 &lt;= nums[i] &lt;= 10^9<\/code><\/li>\n<li><code>-10^9 &lt;= target &lt;= 10^9<\/code><\/li>\n<li>\u53ea\u4f1a\u5b58\u5728\u4e00\u4e2a\u6709\u6548\u7b54\u6848<\/li>\n<\/ul>\n<p>\u8fdb\u9636\uff1a\u4f60\u53ef\u4ee5\u60f3\u51fa\u4e00\u4e2a\u65f6\u95f4\u590d\u6742\u5ea6\u5c0f\u4e8e <code>O(n^2)<\/code> \u7684\u7b97\u6cd5\u5417\uff1f<\/p>\n<hr \/>\n<p>\u6b64\u4e43 Leetcode \u7b2c\u4e00\u9898\uff0c\u641c\u9898\u89e3\u627e\u5230\u4e86<a href=\"https:\/\/programmercarl.com\/0001.%E4%B8%A4%E6%95%B0%E4%B9%8B%E5%92%8C.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE\">\u4ee3\u7801\u968f\u60f3\u5f55\u7684\u8bb2\u89e3<\/a>\uff0c\u6309\u7167\u5176\u4e2d\u7684\u8bb2\u89e3\u8ddf\u7740\u6572\u4e86\u4e00\u904d\uff0c\u987a\u5e26\u5b66\u4e60\u4e86\u4e00\u4e0b<a href=\"https:\/\/programmercarl.com\/%E5%93%88%E5%B8%8C%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html#%E5%93%88%E5%B8%8C%E8%A1%A8\">\u54c8\u5e0c\u8868\u7684\u77e5\u8bc6<\/a>\u3002\uff08\u540c\u65f6\u53c2\u8003\u4e86\u8fd9\u4e2a<a href=\"https:\/\/www.bilibili.com\/video\/BV1SZ4y1z7wT\">\u5173\u4e8e\u54c8\u5e0c\u8868\u7684\u89c6\u9891<\/a>\uff09<\/p>\n<h2><a id=\"%E5%85%88%E6%9D%A5%E7%9C%8B%E4%BB%A3%E7%A0%81%E9%9A%8F%E6%83%B3%E5%BD%95%E4%B8%8A%E6%80%BB%E7%BB%93%E4%B8%AD%E7%9A%84%E5%9B%9B%E4%B8%AA%E9%97%AE%E9%A2%98\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5148\u6765\u770b\u4ee3\u7801\u968f\u60f3\u5f55\u4e0a\u603b\u7ed3\u4e2d\u7684\u56db\u4e2a\u95ee\u9898<\/h2>\n<h3><a id=\"%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BC%9A%E6%83%B3%E5%88%B0%E7%94%A8%E5%93%88%E5%B8%8C%E8%A1%A8%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e3a\u4ec0\u4e48\u4f1a\u60f3\u5230\u7528\u54c8\u5e0c\u8868\uff1f<\/h3>\n<p>\u67e5\u8be2\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u51fa\u73b0\u8fc7\uff0c\u6216\u662f\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u96c6\u5408\u4e2d\u65f6\uff0c\u6bd4\u5982\u4f60\u8981\u7684\u548c\u4e3a9\uff0c\u73b0\u5728\u904d\u5386\u5230\u4e865\uff0c\u5c31\u8981\u601d\u8003\u4e4b\u524d\u662f\u5426\u904d\u5386\u8fc74\u3002<\/p>\n<ul>\n<li>\u5728\u904d\u5386\u7684\u540c\u65f6\uff0c\u8bb0\u5f55\u4e00\u4e9b\u4fe1\u606f\uff0c\u4ee5\u7701\u53bb\u4e00\u5c42\u5faa\u73af\uff0c\u8fd9\u662f\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u7684\u60f3\u6cd5\u3002<\/li>\n<li>\u9700\u8981\u8bb0\u5f55\u5df2\u7ecf\u904d\u5386\u8fc7\u7684\u6570\u503c\u548c\u5b83\u6240\u5bf9\u5e94\u7684\u4e0b\u6807\uff0c\u53ef\u4ee5\u501f\u52a9\u67e5\u627e\u8868\u5b9e\u73b0\u3002<\/li>\n<li>\u9898\u76ee\u6ca1\u6709\u8981\u6c42key\uff08\u6570\u7ec4\u5143\u7d20\uff09\u6709\u5e8f\uff0c\u6240\u4ee5\u4f7f\u7528<strong>\u54c8\u5e0c\u8868<\/strong>\u800c\u4e0d\u662f\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\u3002\uff08\u8fd9\u662f\u4e24\u4e2a\u5e38\u7528\u7684<strong>\u67e5\u627e\u8868<\/strong>\u7684\u5b9e\u73b0\uff09<\/li>\n<\/ul>\n<h3><a id=\"%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%BA%E4%BB%80%E4%B9%88%E7%94%A8map%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u54c8\u5e0c\u8868\u4e3a\u4ec0\u4e48\u7528map\uff1f<\/h3>\n<ul>\n<li>\u56e0\u4e3a\u672c\u9898\uff0c\u6211\u4eec\u4e0d\u4ec5\u8981\u77e5\u9053\u5143\u7d20\u6709\u6ca1\u6709\u904d\u5386\u8fc7\uff0c\u8fd8\u8981\u77e5\u9053\u8fd9\u4e2a\u5143\u7d20\u5bf9\u5e94\u7684\u4e0b\u6807\uff0c\u9700\u8981\u4f7f\u7528 key value\u7ed3\u6784\u6765\u5b58\u653e\uff0ckey\u6765\u5b58\u5143\u7d20\uff0cvalue\u6765\u5b58\u4e0b\u6807\uff0c\u90a3\u4e48\u4f7f\u7528map\u6b63\u5408\u9002\u3002<\/li>\n<\/ul>\n<h3><a id=\"%E6%9C%AC%E9%A2%98map%E6%98%AF%E7%94%A8%E6%9D%A5%E5%AD%98%E4%BB%80%E4%B9%88%E7%9A%84%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u672c\u9898map\u662f\u7528\u6765\u5b58\u4ec0\u4e48\u7684\uff1f<\/h3>\n<ul>\n<li>\u7528\u6765\u5b58\u653e\u6211\u4eec\u4fbf\u5229\u8bbf\u95ee\u8fc7\u7684\u5143\u7d20\uff0c\u56e0\u4e3a\u904d\u5386\u6570\u7ec4\u7684\u65f6\u5019\uff0c\u9700\u8981\u8bb0\u5f55\u6211\u4eec\u4e4b\u524d\u904d\u5386\u8fc7\u54ea\u4e9b<strong>\u5143\u7d20<\/strong>\u548c<strong>\u5bf9\u5e94\u7684\u4e0b\u6807<\/strong>\uff0c\u8fd9\u6837\u624d\u80fd\u627e\u5230\u4e0e\u5f53\u524d\u5143\u7d20\u76f8\u5339\u914d\u7684\uff08\u4e5f\u5c31\u662f\u76f8\u52a0\u7b49\u4e8etarget\uff09<\/li>\n<\/ul>\n<h3><a id=\"map%E4%B8%AD%E7%9A%84key%E5%92%8Cvalue%E7%94%A8%E6%9D%A5%E5%AD%98%E4%BB%80%E4%B9%88%E7%9A%84\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>map\u4e2d\u7684key\u548cvalue\u7528\u6765\u5b58\u4ec0\u4e48\u7684<\/h3>\n<ul>\n<li>{key\uff1a\u6570\u7ec4\u5143\u7d20\uff0cvalue\uff1a\u6570\u7ec4\u5143\u7d20\u5bf9\u5e94\u7684\u4e0b\u6807}<\/li>\n<li>\u9898\u76ee\u8981\u6c42\u67e5\u627e<strong>\u6570\u7ec4\u5143\u7d20<\/strong>\u662f\u5426\u51fa\u73b0\u8fc7\uff0c\u6240\u4ee5\u6570\u7ec4\u5143\u7d20\u4e3akey<\/li>\n<\/ul>\n<h2><a id=\"%E6%80%9D%E8%B7%AF\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u601d\u8def<\/h2>\n<h3><a id=\"%E5%A4%8D%E6%9D%82%E5%BA%A6%E5%88%86%E6%9E%90\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n<p>\u4e24\u5c42for\u5faa\u73af\u7684\u66b4\u529b\u89e3\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\u3002\u901a\u8fc7\u54c8\u5e0c\u8868\u53ef\u4ee5\u5c06\u5bfb\u627etarget &#8211; nums[i] \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53d8\u4e3aO(1)\uff0c\u56e0\u4e3a\u6bcf\u6b21\u904d\u5386\u540e\u90fd\u4f1a\u628a\u904d\u5386\u8fc7\u7684\u5143\u7d20\u653e\u8fdb\u96c6\u5408map\u4e2d\uff0c\u90a3\u4e48\u6211\u4eec\u6700\u5dee\u7684\u60c5\u51b5\u4e5f\u5c31\u662f\u904d\u5386\u5230\u6570\u7ec4\u4e2d\u6700\u540e\u4e00\u4e2a\u6570\u624d\u4f1a\u5728map\u4e2d\u627e\u5230\u5bf9\u5e94\u7684\u6570\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u8ddf\u5143\u7d20\u6570\u91cf\uff08\u6216\u8005\u8bf4\u6570\u7ec4\u957f\u5ea6\uff09\u6709\u5173\uff0c\u5373\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)<\/p>\n<h3><a id=\"%E8%BF%87%E7%A8%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8fc7\u7a0b<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/code-thinking-1253855093.file.myqcloud.com\/pics\/20220711202638.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/code-thinking-1253855093.file.myqcloud.com\/pics\/20230220223536.png\" alt=\"\" \/><\/p>\n<h3><a id=\"java%E4%BB%A3%E7%A0%81\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Java \u4ee3\u7801<\/h3>\n<pre><code class=\"language-java\">class Solution {\n    public int[] twoSum(int[] nums, int target) {\n        \/\/ \u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a2\u7684\u7ed3\u679c\u6570\u7ec4res\uff0c\u5e76\u68c0\u67e5\u8f93\u5165\u6570\u7ec4nums\u662f\u5426\u4e3a\u7a7a\u6216\u957f\u5ea6\u4e3a0\u3002\u5982\u679c\u662f\uff0c\u5c31\u76f4\u63a5\u8fd4\u56deres\n        int[] res = new int[2];\n        if (nums == null || nums.length == 0){\n            return res;\n        } \n        Map&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();\/\/ \u521b\u5efa\u4e00\u4e2a\u54c8\u5e0c\u6620\u5c04map\uff0c\u7528\u4e8e\u5b58\u50a8\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u548c\u5b83\u7684\u7d22\u5f15\u3002\n        \/\/ \u904d\u5386\u8f93\u5165\u6570\u7ec4nums\u3002\u5bf9\u4e8e\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u8ba1\u7b97target\u51cf\u53bb\u8be5\u5143\u7d20\u7684\u503c\uff0c\u5f97\u5230temp\u3002\n        for(int i = 0; i &lt; nums.length; i++){\n        int temp = target - nums[i];\n        \/\/ \u68c0\u67e5map\u4e2d\u662f\u5426\u5305\u542btemp\u4f5c\u4e3a\u952e\u3002\u5982\u679c\u5305\u542b\uff0c\u8bf4\u660e\u627e\u5230\u4e86\u4e24\u4e2a\u6570\u7684\u548c\u7b49\u4e8etarget\uff0c\u8fd9\u4e24\u4e2a\u6570\u5c31\u662f\u5f53\u524d\u5143\u7d20\u548cmap\u4e2d\u952e\u4e3atemp\u7684\u5143\u7d20\u3002\u5c06\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u5206\u522b\u5b58\u50a8\u5728res[1]\u548cres[0]\u4e2d\uff0c\u7136\u540e\u8df3\u51fa\u5faa\u73af\u3002\n        if(map.containsKey(temp)){\n            res[1] = i;\n            res[0] = map.get(temp);\n            break;\n        }\n        map.put(nums[i], i);\/\/ \u5982\u679cmap\u4e2d\u4e0d\u5305\u542btemp\uff0c\u5c31\u5c06\u5f53\u524d\u5143\u7d20\u548c\u5b83\u7684\u7d22\u5f15\u6dfb\u52a0\u5230map\u4e2d\u3002 \n        }\n        return res;\/\/ \u904d\u5386\u7ed3\u675f\u540e\uff0c\u8fd4\u56de\u7ed3\u679c\u6570\u7ec4res\u3002 \n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c target\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa \u548c\u4e3a\u76ee\u6807\u503c target \u7684\u90a3 \u4e24\u4e2a \u6574\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u4e0b\u6807\u3002 \u4f60\u53ef\u4ee5\u5047\u8bbe\u6bcf\u79cd\u8f93\u5165\u53ea\u4f1a\u5bf9\u5e94\u4e00\u4e2a\u7b54\u6848\u3002\u4f46\u662f\uff0c\u6570\u7ec4\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-code-thinking-note","tag-suanfa"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":2}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.qicy.top\/index.php?rest_route=\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qicy.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qicy.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qicy.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qicy.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=97"}],"version-history":[{"count":3,"href":"https:\/\/www.qicy.top\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":103,"href":"https:\/\/www.qicy.top\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions\/103"}],"wp:attachment":[{"href":"https:\/\/www.qicy.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qicy.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qicy.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}