{"id":1942,"date":"2022-05-03T13:30:52","date_gmt":"2022-05-03T13:30:52","guid":{"rendered":"https:\/\/www.zttofficial.com\/?p=1942"},"modified":"2022-05-10T15:51:44","modified_gmt":"2022-05-10T15:51:44","slug":"%e5%81%9a%e9%a1%8c%e7%ad%86%e8%a8%98%ef%bc%9atwo-sumjava","status":"publish","type":"post","link":"https:\/\/www.zttofficial.com\/?p=1942","title":{"rendered":"\u505a\u984c\u7b46\u8a18\uff1aTwo Sum (Java)"},"content":{"rendered":"\n<p>\u9a5a\u6050\u81ea\u5df1\u8981\u627e\u4e0d\u5230\u5de5\u4f5c\u4e86\uff0c\u958b\u59cb\u505a\u984c\uff0c\u8a18\u9304\u4e00\u4e0b\u505a\u984c\u7b46\u8a18\u3002\u7b2c\u4e00\u9053\uff0c\u662f\u5237\u984c\u754c\u7684 Hello World \u2014\u2014 Two Sum\u3002<\/p>\n\n\n\n<p><strong>\u984c\u76ee\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<p>\u7d66\u5b9a\u4e00\u500b\u6574\u6578\u6578\u7d44  <kbd>nums<\/kbd> \u548c\u4e00\u500b\u6574\u6578 <kbd>target<\/kbd>\uff0c\u8fd4\u56de\u5169\u500b\u6578\u5b57\u7684\u7d22\u5f15\uff0c\u4f7f\u5b83\u5011\u76f8\u52a0\u70ba <kbd>target<\/kbd>\u3002<\/p>\n\n\n\n<p>\u5047\u8a2d\u6bcf\u500b\u8f38\u5165\u90fd\u53ea\u6709\u4e00\u500b\u89e3\u6c7a\u65b9\u6848\uff0c\u4e26\u4e14\u4e0d\u6703\u4f7f\u7528\u76f8\u540c\u7684\u5143\u7d20\u5169\u6b21\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u6309\u4efb\u4f55\u9806\u5e8f\u8fd4\u56de\u7b54\u6848\u3002<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: nums = [2,7,11,15], target = 9\nOutput: [0,1]\nExplanation: Because nums[0] + nums[1] == 9, we return [0, 1].<\/code><\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: nums = [3,2,4], target = 6\nOutput: [1,2]<\/code><\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: nums = [3,3], target = 6\nOutput: [0,1]<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Foreach \u66b4\u529b\u89e3\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p>\u7b2c\u4e00\u6b21\u5237\u984c\uff0c\u7b97\u6cd5\u5c0f\u767d\uff0c\u53ea\u60f3\u5f97\u5230\u9019\u7a2e\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java line-numbers\">class Solution {\n    public int[] twoSum(int[] nums, int target) {\n        int [] empty = new int[2];\n        int len = nums.length;\n        for(int i = 0; i &lt; len; i++){\n            for(int j = 0; j &lt; len; j++){\n                    if (nums[i]+nums[j] == target&amp;&amp; i != j){\n                        empty[0] = i;\n                        empty[1] = j;\n                        return empty;\n                    }\n            }\n        }\n        return null;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u679c\u4e0d\u5176\u7136\uff0c106 ms\uff0c\u8d85\u904e\u4e8610%\u7684\u7b54\u6848\uff0c\u597d\u6b79\u662f\u901a\u904e\u4e86\u5c0d\u5427\u2026\u2026<\/p>\n\n\n\n<p>\u4ee5\u524d\u4e0a\u8ab2\u505a\u4f5c\u696d\u6642\uff0c\u90fd\u662f\u79c9\u6301\u8457\u80fd\u8dd1\u5c31\u884c\u7684\u614b\u5ea6\uff0c\u7fd2\u6163\u5f88\u5dee\uff0c\u73fe\u5728\u6211\u958b\u59cb\u8a8d\u771f\u8003\u616e\u512a\u5316\u7684\u554f\u984c\u4e86\uff0c\u4e5f\u7b97\u662f\u4e00\u5927\u9032\u6b65\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Hashmap \u89e3\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p>\u770b\u4e86 <a href=\"https:\/\/leetcode.com\/problems\/two-sum\/discuss\/3\/Accepted-Java-O(n)-Solution\" data-type=\"URL\" data-id=\"https:\/\/leetcode.com\/problems\/two-sum\/discuss\/3\/Accepted-Java-O(n)-Solution\" target=\"_blank\" rel=\"noreferrer noopener\">discussion<\/a> \u4e4b\u5f8c\uff0c\u8ffd\u6c42\u9ad8\u6548\u7387\uff0c\u9019\u9053\u984c\u662f\u61c9\u8a72\u4f7f\u7528 Hashmap \u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java line-numbers\">class Solution {\n    public int[] twoSum(int[] numbers, int target) {\n        int[] result = new int[2];\n        Map&lt;Integer, Integer&gt; map = new HashMap&lt;Integer, Integer&gt;();\n        for (int i = 0; i &lt; numbers.length; i++) {\n            if (map.containsKey(target - numbers[i])) {\n                result[1] = i;\n                result[0] = map.get(target - numbers[i]);\n                return result;\n            }\n        map.put(numbers[i], i);\n    }\n    return result;\n}\n}<\/code><\/pre>\n\n\n\n<p>3 ms\uff0c\u8d85\u904e85%\u7684\u7d50\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9a5a\u6050\u81ea\u5df1\u8981\u627e\u4e0d\u5230\u5de5\u4f5c\u4e86\uff0c\u958b\u59cb\u505a\u984c\uff0c\u8a18\u9304\u4e00\u4e0b\u505a\u984c\u7b46\u8a18\u3002\u7b2c\u4e00\u9053\uff0c\u662f\u5237\u984c\u754c\u7684 Hello World \u2014\u2014 Two Sum\u3002 \u984c\u76ee\u63cf\u8ff0\uff1a \u7d66\u5b9a\u4e00\u500b\u6574\u6578\u6578\u7d44 nums \u548c\u4e00\u500b\u6574\u6578 target\uff0c\u8fd4\u56de\u5169\u500b\u6578\u5b57\u7684\u7d22\u5f15\uff0c\u4f7f\u5b83\u5011\u76f8\u52a0\u70ba target\u3002 \u5047\u8a2d\u6bcf\u500b\u8f38\u5165\u90fd\u53ea\u6709\u4e00\u500b\u89e3\u6c7a\u65b9\u6848\uff0c\u4e26\u4e14\u4e0d\u6703\u4f7f\u7528\u76f8\u540c\u7684\u5143\u7d20\u5169\u6b21\u3002 \u53ef\u4ee5\u6309\u4efb\u4f55\u9806\u5e8f\u8fd4\u56de\u7b54\u6848\u3002 Example 1: Example 2: Example 3: Foreach \u66b4\u529b\u89e3\u6cd5\uff1a \u7b2c\u4e00\u6b21\u5237\u984c\uff0c\u7b97\u6cd5\u5c0f\u767d\uff0c\u53ea\u60f3\u5f97\u5230\u9019\u7a2e\u65b9\u5f0f\uff1a \u679c\u4e0d\u5176\u7136\uff0c106 ms\uff0c\u8d85\u904e\u4e8610%\u7684\u7b54\u6848\uff0c\u597d\u6b79\u662f\u901a\u904e\u4e86\u5c0d\u5427\u2026\u2026 \u4ee5\u524d\u4e0a\u8ab2\u505a\u4f5c\u696d\u6642\uff0c\u90fd\u662f\u79c9\u6301\u8457\u80fd\u8dd1\u5c31\u884c\u7684\u614b\u5ea6\uff0c\u7fd2\u6163\u5f88\u5dee\uff0c\u73fe\u5728\u6211\u958b\u59cb\u8a8d\u771f\u8003\u616e\u512a\u5316\u7684\u554f\u984c\u4e86\uff0c\u4e5f\u7b97\u662f\u4e00\u5927\u9032\u6b65\u3002 Hashmap \u89e3\u6cd5\uff1a \u770b\u4e86 discussion \u4e4b\u5f8c\uff0c\u8ffd\u6c42\u9ad8\u6548\u7387\uff0c\u9019\u9053\u984c\u662f\u61c9\u8a72\u4f7f\u7528 Hashmap \u7684\uff1a 3 ms\uff0c\u8d85\u904e85%\u7684\u7d50\u679c\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,135],"tags":[138,137,136],"class_list":["post-1942","post","type-post","status-publish","format-standard","hentry","category-all","category-135","tag-hashmap","tag-leetcode","tag-136"],"_links":{"self":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/1942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1942"}],"version-history":[{"count":6,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/1942\/revisions"}],"predecessor-version":[{"id":1961,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/1942\/revisions\/1961"}],"wp:attachment":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}