{"id":2696,"date":"2020-01-09T10:37:08","date_gmt":"2020-01-09T02:37:08","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2696"},"modified":"2020-01-09T10:37:08","modified_gmt":"2020-01-09T02:37:08","slug":"%e3%80%90leetcode%e3%80%910001%e4%b8%a4%e6%95%b0%e4%b9%8b%e5%92%8c","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2696\/","title":{"rendered":"\u3010LeetCode\u30110001\u4e24\u6570\u4e4b\u548c"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/two-sum\">https:\/\/leetcode-cn.com\/problems\/two-sum<\/a><\/p>\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\nclass Solution(object):\n    def twoSum(self, nums, target):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> nums: List[int]\n        <\/em><strong><em>:type<\/em><\/strong><em> target: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: List[int]\n        \"\"\"\n        <\/em>temp = [target - nums[i] for i in range(len(nums))]\n        for i in range(len(nums)):\n            flag = nums[i]\n            for j in range(len(temp)):\n                if temp[j] == flag and i != j:\n                    return [i, j]\nif __name__ == '__main__':\n    nums = [2, 7, 11, 15]\n    nums = [3, 2, 4]\n    target = 6\n    print(Solution().twoSum(nums, target))\n<\/pre>\n\n\n<p><strong>\u601d\u8def\uff1a<\/strong>\u8fd9\u9053\u9898\u6211\u505a\u7684\u65f6\u5019\u5012\u662f\u6ca1\u7528\u4ec0\u4e48\u5177\u4f53\u7684\u7b97\u6cd5\uff0c\u53ea\u662f\u5229\u7528\u4e00\u4e2a\u8f85\u52a9\u6570\u7ec4\uff0c\u5176\u503c\u4e3atarget &#8211; nums[i]\uff0c\u7136\u540e\u5bf9\u6bd4\u4e24\u4e2a\u5217\u8868\u4e2d\u662f\u5426\u6709\u76f8\u540c\u7684\u5143\u7d20\uff0c\u9700\u8981\u6ce8\u610f\u7684\u5c31\u662f\u7b2c\u4e8c\u5c42\u5faa\u73af\u4e2d\u7684\u5224\u65ad\u9700\u8981\u52a0  <strong>i != j<\/strong>  \u4ee5\u9632\u6b62\u81ea\u6bd4\u8f83\u95ee\u9898\uff0c\u7b2c\u4e00\u6b21\u63d0\u4ea4\u5c31\u662f\u56e0\u4e3a\u6ca1\u6709\u60f3\u5230\u8fd9\u4e2a\u7ed3\u679c\u9519\u4e86\u3002<\/p>\n\n\n<p><strong>\u601d\u8003\uff1a<\/strong>\u4f46\u662f\u6211\u8fd9\u91cc\u7684\u5bf9\u6bd4\u5217\u8868\u7528\u7684\u662f\u4e00\u4e2a\u53cc\u5c42\u5faa\u73af\uff0c\u8fd9\u4f7f\u5f97\u6574\u4e2a\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0a\u5347\u5230\u4e86n^2\uff0c\u67e5\u770b\u8bc4\u8bba\u540e\uff0c\u5bf9\u6bd4\u53ef\u4ee5\u6362\u6210\u5b57\u5178\uff0c\u8fd9\u6837\u5728\u5b57\u5178\u4e2d\u67e5\u627e\u603b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u4f1a\u964d\u5230nlgn\u3002<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def twoSum(self, nums, target):\n    <em>\"\"\"\n    <\/em><strong><em>:type<\/em><\/strong><em> nums: List[int]\n    <\/em><strong><em>:type<\/em><\/strong><em> target: int\n    <\/em><strong><em>:rtype<\/em><\/strong><em>: List[int]\n    \"\"\"\n    <\/em>hashmap = {}\n    for index, num in enumerate(nums):\n        another_num = target - num\n        if another_num in hashmap:\n            return [hashmap[another_num], index]\n        hashmap[num] = index\n    return None<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/two-sum \u4ee3\u7801\uff1a # -*- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1777,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2696"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2696"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2696\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2696"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}