{"id":355,"date":"2021-12-07T03:17:10","date_gmt":"2021-12-07T03:17:10","guid":{"rendered":"https:\/\/blog.liuyingjie.com.cn\/?p=355"},"modified":"2023-08-13T07:51:42","modified_gmt":"2023-08-13T07:51:42","slug":"leetcode-question-bank-solution","status":"publish","type":"post","link":"https:\/\/blog.liuyingjie.com.cn\/?p=355","title":{"rendered":"1. \u4e24\u6570\u4e4b\u548c"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e<\/strong><\/h2>\n\n\n\n<p>\u6700\u5bb9\u6613\u60f3\u5230\u7684\u65b9\u6cd5\u662f\u679a\u4e3e\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570<code>x<\/code>\uff0c\u5bfb\u627e\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728 <code>target - x<\/code>\u3002<\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u4f7f\u7528\u904d\u5386\u6574\u4e2a\u6570\u7ec4\u7684\u65b9\u5f0f\u5bfb\u627e <code>target - x<\/code> \u65f6\uff0c\u9700\u8981\u6ce8\u610f\u5230\u6bcf\u4e00\u4e2a\u4f4d\u4e8e <code>x<\/code><\/p>\n\n\n\n<p>C \u4ee3\u7801\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">\/**\n * Note: The returned array must be malloced, assume caller calls free().\n *\/\nint* twoSum(int* nums, int numsSize, int target, int* returnSize){\n    for (int i = 0; i &lt; numsSize; ++i){\n        for(int j = i + 1; j &lt; numsSize; ++j){\n            if (nums[i] + nums[j] == target){\n                int* ret = malloc(sizeof(int) * 2);\n                ret[0] = i;\n                ret[1] = j;\n                *returnSize = 2;\n                return ret;\n            }\n        }\n    }\n    *returnSize = 0;\n    return NULL;\n}<\/code><\/pre>\n\n\n\n<p>C++ \u4ee3\u7801\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">class Solution {\npublic:\n    vector&lt;int&gt; twoSum(vector&lt;int&gt;&amp; nums, int target) {\n        int n = nums.size();\n        for (int i = 0; i &lt; n; ++i) {\n            for (int j = i + 1; j &lt; n; ++j) {\n                if (nums[i] + nums[j] == target) {\n                    return {i, j};\n                }\n            }\n        }\n        return {};\n    }\n};<\/code><\/pre>\n\n\n\n<p>Python3 \u4ee3\u7801\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">class Solution:\n    def twoSum(self, nums: List[int], target: int) -&gt; List[int]:\n        n = len(nums)\n        for i in range(n):\n            for j in range(i + 1, n):\n                if nums[i] + nums[j] == target:\n                    return [i,j]\n        return []<\/code><\/pre>\n\n\n\n<p>Java \u4ee3\u7801\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">class Solution {\n    public int[] twoSum(int[] nums, int target) {\n        int n = nums.length;\n        for(int i = 0; i &lt; n; ++i){\n            for(int j = i + 1; j &lt; n; ++j){\n                if(nums[0] + nums[1] == target){\n                    return new int[]{i,j};\n                }\n            }\n        }\n        return new int[0];\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u590d\u6742\u5ea6\u5206\u6790 \u65f6\u95f4\u590d\u6742\u5ea6\uff1a<em>O<\/em>(<em>N<\/em>^2)\uff0c\u5176\u4e2d <em>N<\/em> \u662f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u3002\u6700\u574f\u60c5\u51b5\u4e0b\u6570\u7ec4\u4e2d\u4efb\u610f\u4e24\u4e2a\u6570\u90fd\u8981\u88ab\u5339\u914d\u4e00\u6b21\u3002 \u7a7a\u95f4\u590d\u6742\u5ea6\uff1a<em>O<\/em>(1)\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u65b9\u6cd5\u4e8c\uff1a\u54c8\u5e0c\u8868<\/strong><\/h2>\n\n\n\n<p>\u6ce8\u610f\u5230\u65b9\u6cd5\u4e00\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\u7684\u539f\u56e0\u662f\u5bfb\u627e\u5230 <code>target - x<\/code> \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8fc7\u9ad8\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u66f4\u4f18\u79c0\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u5feb\u901f\u5bfb\u627e\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u76ee\u6807\u5143\u7d20\u3002\u5982\u679c\u5b58\u5728\uff0c\u6211\u4eec\u9700\u8981\u627e\u51fa\u4ed6\u7684\u7d22\u5f15\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u54c8\u5e0c\u8868\uff0c\u53ef\u4ee5\u5c06\u5bfb\u627e <code>target - x<\/code> \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u5230\u4ece <em>O<\/em>(<em>N<\/em>) \u964d\u4f4e\u5230 <em>O<\/em>(<em>1<\/em>) \u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u4e2a <code>x<\/code> \uff0c\u6211\u4eec\u9996\u5148\u67e5\u8be2\u54c8\u5e0c\u8868\u4e2d\u662f\u5426\u5b58\u5728 <code>target - x<\/code>\uff0c\u7136\u540e\u5c06 <code>x<\/code> \u63d2\u5165\u5230\u54c8\u5e0c\u8868\u4e2d\uff0c\u5373\u53ef\u4fdd\u8bc1\u4e0d\u4f1a\u8ba9 <code>x<\/code> \u548c\u81ea\u5df1\u5339\u914d\u3002<\/p>\n\n\n\n<p>Python \u4ee3\u7801\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">class Solution:\n    def twoSum(self, nums: List[int], target: int) -&gt; List[int]:\n        hashtable = dict()\n        for i, num in enumerate(nums):\n            if target - num in hashtable:\n                return [hashtable[target - num], i]\n            hashtable[nums[i]] = i\n        return []<\/code><\/pre>\n\n\n\n<p>Java \u4ee3\u7801\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">class Solution {\n    public int[] twoSum(int[] nums, int target) {\n        Map&lt;Integer, Integer&gt; hashtable = new HashMap&lt;Integer, Integer&gt;();\n        for (int i = 0; i &lt; nums.length; ++i) {\n            if (hashtable.containsKey(target - nums[i])) {\n                return new int[]{hashtable.get(target - nums[i]), i};\n            }\n            hashtable.put(nums[i], i);\n        }\n        return new int[0];\n    }   \n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.liuyingjie.com.cn\/wp-content\/uploads\/2022\/06\/1.Two-Sum-scaled.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Source: LeetCode\uff08The title reproduced in this blog is for personal study use only\uff09<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e \u6700\u5bb9\u6613\u60f3\u5230\u7684\u65b9\u6cd5\u662f\u679a\u4e3e\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570x\uff0c\u5bfb\u627e\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728 target &#8211; x\u3002 \u5f53\u6211\u4eec &hellip; <a href=\"https:\/\/blog.liuyingjie.com.cn\/?p=355\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">1. \u4e24\u6570\u4e4b\u548c<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-355","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=355"}],"version-history":[{"count":51,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":965,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions\/965"}],"wp:attachment":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}