{"id":49,"date":"2021-12-11T20:17:14","date_gmt":"2021-12-11T12:17:14","guid":{"rendered":"http:\/\/139.9.1.231\/?p=49"},"modified":"2021-12-12T18:31:17","modified_gmt":"2021-12-12T10:31:17","slug":"leetcode-day1","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2021\/12\/11\/leetcode-day1\/","title":{"rendered":"leetcode day1 \u4e24\u6570\u4e4b\u548c"},"content":{"rendered":"\n<p><strong>&nbsp;     \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums&nbsp;\u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c target<\/strong>\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa \u548c\u4e3a\u76ee\u6807\u503c target&nbsp; \u7684\u90a3&nbsp;\u4e24\u4e2a&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\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\n\n\n<p>\u4f60\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8f93\u5165\uff1anums = [2,7,11,15], target = 9\n\u8f93\u51fa\uff1a[0,1]\n\u89e3\u91ca\uff1a\u56e0\u4e3a nums[0] + nums[1] == 9 \uff0c\u8fd4\u56de [0, 1] \u3002<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8f93\u5165\uff1anums = [3,2,4], target = 6\n\u8f93\u51fa\uff1a[1,2]<\/code><\/pre>\n\n\n\n<p>\u89e3\u9898\u601d\u8def<br>        1 \u66b4\u529b\u89e3\u6cd5\uff1a\u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab\u679a\u4e3e\u591a\u6b21\uff0c\u7b97\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\u4e3ao(n^2)<\/p>\n\n\n\n<p>2\u7528 Python \u4e2d list \u7684\u76f8\u5173\u51fd\u6570\u6c42\u89e3<\/p>\n\n\n\n<p>\u89e3\u9898\u5173\u952e\u4e3b\u8981\u662f\u60f3\u627e\u5230 num2 = target &#8211; num1\uff0c\u662f\u5426\u4e5f\u5728 list \u4e2d\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u8fd0\u7528\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>num2 in nums\uff0c\u8fd4\u56de True \u8bf4\u660e\u6709\u620f<br>nums.index(num2)\uff0c\u67e5\u627e num2 \u7684\u7d22\u5f15<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def twoSum(nums, target):\n    lens = len(nums)\n    j=-1\n    for i in range(lens):\n        if (target - nums[i]) in nums:\n            if (nums.count(target - nums[i]) == 1)&amp;(target - nums[i] == nums[i]):#\u5982\u679cnum2=num1,\u4e14nums\u4e2d\u53ea\u51fa\u73b0\u4e86\u4e00\u6b21\uff0c\u8bf4\u660e\u627e\u5230\u662fnum1\u672c\u8eab\u3002\n                continue\n            else:\n                j = nums.index(target - nums[i],i+1) #index(x,i+1)\u662f\u4ecenum1\u540e\u7684\u5e8f\u5217\u540e\u627enum2                \n                break\n    if j>0:\n        return [i,j]\n    else:\n        return []<\/code><\/pre>\n\n\n\n<p>\u54c8\u5e0c\u8868\uff1a<strong>\u7c7b\u4f3c\u5b57\u5178\uff0ckey:value<\/strong><br> \u54c8\u5e0c\u89e3\u6cd5\uff1a\u53ea\u8981\u4e00\u6b21\u904d\u5386\uff0c\u904d\u5386\u5230i\u65f6\uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u627etarget-i\u662f\u5426\u5b58\u5728\u5373\u53ef<br> \u82e5target-i\u5b58\u5728\uff1a\u8fd4\u56dei\u548ctarget-i\u7684\u4e0b\u6807<br> \u82e5target-i\u4e0d\u5728\uff1a\u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u7b2ci\u4e2a\u5143\u7d20<br> \u65f6\u95f4\uff1ao(n)<br> \u7a7a\u95f4\uff1ao(n)<\/p>\n\n\n\n<p>\u89e3\u9898\u601d\u8def\uff1a\u54c8\u5e0c\u8868<\/p>\n\n\n\n<p>      \u5982\u679c\u6211\u4eec\u4f7f\u7528\u66b4\u7834\uff0c\u4f1a\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a n^2\u8fd9\u6837\u7684\u4ee3\u4ef7\u65e0\u7591\u662f\u5f88\u5927\u7684\u3002 \u6240\u4ee5\u6211\u4eec\u5f88\u5bb9\u6613\u60f3\u5230\u7528\u54c8\u5e0c\u8868\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002  <br>     \u6211\u4eec\u904d\u5386\u5230\u6570\u5b57 a \u65f6\uff0c\u7528 target \u51cf\u53bb a\uff0c\u5c31\u4f1a\u5f97\u5230 b\uff0c\u82e5 b \u5b58\u5728\u4e8e\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u4e86\u3002\u82e5 b \u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u6211\u4eec\u9700\u8981\u5c06 a \u5b58\u5165\u54c8\u5e0c\u8868\uff0c\u597d\u8ba9\u540e\u7eed\u904d\u5386\u7684\u6570\u5b57\u4f7f\u7528\u3002 <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def twoSum(self, nums: List[int], target: int) -> 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[num]=i\n# @lc code=end<\/code><\/pre>\n\n\n\n<p><strong>\u8865\u5145\u77e5\u8bc6\uff1a<\/strong><\/p>\n\n\n\n<h3>\u200bPython 3 \u65b0\u7279\u6027\uff1a\u7c7b\u578b\u6ce8\u89e3<\/h3>\n\n\n\n<p>\u524d\u51e0\u5929\u6709\u540c\u5b66\u95ee\u5230\uff0c\u8fd9\u4e2a\u5199\u6cd5\u662f\u4ec0\u4e48\u610f\u601d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(x:int, y:int) -> int:\n    return x + y<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053 Python \u662f\u4e00\u79cd\u52a8\u6001\u8bed\u8a00\uff0c\u53d8\u91cf\u4ee5\u53ca\u51fd\u6570\u7684\u53c2\u6570\u662f<strong>\u4e0d\u533a\u5206\u7c7b\u578b<\/strong>\u3002\u56e0\u6b64\u6211\u4eec\u5b9a\u4e49\u51fd\u6570\u53ea\u9700\u8981\u8fd9\u6837\u5199\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(x, y):\n    return x + y<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u7684\u597d\u5904\u662f\u6709\u6781\u5927\u7684\u7075\u6d3b\u6027\uff0c\u4f46\u574f\u5904\u5c31\u662f\u5bf9\u4e8e\u522b\u4eba\u4ee3\u7801\uff0c\u65e0\u6cd5\u4e00\u773c\u5224\u65ad\u51fa\u53c2\u6570\u7684\u7c7b\u578b\uff0cIDE&nbsp;\u4e5f\u65e0\u6cd5\u7ed9\u51fa\u6b63\u786e\u7684\u63d0\u793a\u3002<\/p>\n\n\n\n<p>\u4e8e\u662f Python 3 \u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684\u7279\u6027\uff1a<br><strong>\u51fd\u6570\u6ce8\u89e3<\/strong><\/p>\n\n\n\n<p>\u4e5f\u5c31\u662f\u6587\u7ae0\u5f00\u5934\u7684\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(x:int, y:int) -> int:\n    return x + y<\/code><\/pre>\n\n\n\n<p>\u7528&nbsp;<code>: \u7c7b\u578b<\/code>&nbsp;\u7684\u5f62\u5f0f\u6307\u5b9a\u51fd\u6570\u7684<strong>\u53c2\u6570\u7c7b\u578b<\/strong>\uff0c\u7528&nbsp;<code>-&gt; \u7c7b\u578b<\/code>&nbsp;\u7684\u5f62\u5f0f\u6307\u5b9a\u51fd\u6570\u7684<strong>\u8fd4\u56de\u503c<\/strong>\u7c7b\u578b<\/p>\n\n\n\n<p>\u5728 Python 3.6 \u4e2d\uff0c\u53c8\u5f15\u5165\u4e86\u5bf9<strong>\u53d8\u91cf\u7c7b\u578b<\/strong>\u8fdb\u884c\u6ce8\u89e3\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a: int = 123\nb: str = 'hello'<\/code><\/pre>\n\n\n\n<p>\u66f4\u8fdb\u4e00\u6b65\uff0c\u5982\u679c\u4f60\u9700\u8981\u6307\u660e\u4e00\u4e2a\u5168\u90e8\u7531\u6574\u6570\u7ec4\u6210\u7684\u5217\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from typing import List\nl: List[int] = [1, 2, 3]<\/code><\/pre>\n\n\n\n<p>\u4f46\u540c\u6837\uff0c\u8fd9\u4e9b\u4ec5\u4ec5\u662f\u201c<strong>\u6ce8\u89e3<\/strong>\u201d\uff0c\u4e0d\u4f1a\u5bf9\u4ee3\u7801\u4ea7\u751f\u4efb\u4f55\u5f71\u54cd\u3002<\/p>\n\n\n\n<ul><li><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic3.zhimg.com\/v2-10425d851233eebd035f125abffc1731_r.jpg?source=1940ef5c\" alt=\"\"\/><figcaption>                                                                   \u4eba\u751f \u603b\u662f\u8d77\u8d77\u843d\u843d\u843d\u843d\u841d\u8389\u843d\u843d<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-audio aligncenter\"><audio controls src=\"https:\/\/ws.stream.qqmusic.qq.com\/C400002jB3070e7jWc.m4a?guid=73323327&amp;vkey=B8333FD69954F5143E21BEB35AB03A1806A9173AC68C37FB9D3C72135EDBC4EFDF42D08099AE6501CB226BD7606B812695F6D153D0DF7763&amp;uin=626567678&amp;fromtag=143\"><\/audio><figcaption>                                                                        see you again<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums&nbsp;\u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c target\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa \u548c\u4e3a\u76ee &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2021\/12\/11\/leetcode-day1\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcode day1 \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":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/49"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":22,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":79,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions\/79"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}