{"id":1732,"date":"2022-01-10T11:21:16","date_gmt":"2022-01-10T03:21:16","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1732"},"modified":"2022-01-10T11:21:17","modified_gmt":"2022-01-10T03:21:17","slug":"leetcodeday8","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/10\/leetcodeday8\/","title":{"rendered":"leetcodeday8 &#8211;\u5b57\u7b26\u4e32\u8f6c\u6362\u6574\u6570 (atoi)"},"content":{"rendered":"\n<p>\u8bf7\u4f60\u6765\u5b9e\u73b0\u4e00\u4e2a&nbsp;<code>myAtoi(string s)<\/code>&nbsp;\u51fd\u6570\uff0c\u4f7f\u5176\u80fd\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e00\u4e2a 32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff08\u7c7b\u4f3c C\/C++ \u4e2d\u7684&nbsp;<code>atoi<\/code>&nbsp;\u51fd\u6570\uff09\u3002<\/p>\n\n\n\n<p>\u51fd\u6570&nbsp;<code>myAtoi(string s)<\/code>&nbsp;\u7684\u7b97\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul><li>\u8bfb\u5165\u5b57\u7b26\u4e32\u5e76\u4e22\u5f03\u65e0\u7528\u7684\u524d\u5bfc\u7a7a\u683c<\/li><li>\u68c0\u67e5\u4e0b\u4e00\u4e2a\u5b57\u7b26\uff08\u5047\u8bbe\u8fd8\u672a\u5230\u5b57\u7b26\u672b\u5c3e\uff09\u4e3a\u6b63\u8fd8\u662f\u8d1f\u53f7\uff0c\u8bfb\u53d6\u8be5\u5b57\u7b26\uff08\u5982\u679c\u6709\uff09\u3002 \u786e\u5b9a\u6700\u7ec8\u7ed3\u679c\u662f\u8d1f\u6570\u8fd8\u662f\u6b63\u6570\u3002 \u5982\u679c\u4e24\u8005\u90fd\u4e0d\u5b58\u5728\uff0c\u5219\u5047\u5b9a\u7ed3\u679c\u4e3a\u6b63\u3002<\/li><li>\u8bfb\u5165\u4e0b\u4e00\u4e2a\u5b57\u7b26\uff0c\u76f4\u5230\u5230\u8fbe\u4e0b\u4e00\u4e2a\u975e\u6570\u5b57\u5b57\u7b26\u6216\u5230\u8fbe\u8f93\u5165\u7684\u7ed3\u5c3e\u3002\u5b57\u7b26\u4e32\u7684\u5176\u4f59\u90e8\u5206\u5c06\u88ab\u5ffd\u7565\u3002<\/li><li>\u5c06\u524d\u9762\u6b65\u9aa4\u8bfb\u5165\u7684\u8fd9\u4e9b\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\uff08\u5373\uff0c&#8221;123&#8243; -&gt; 123\uff0c &#8220;0032&#8221; -&gt; 32\uff09\u3002\u5982\u679c\u6ca1\u6709\u8bfb\u5165\u6570\u5b57\uff0c\u5219\u6574\u6570\u4e3a&nbsp;<code>0<\/code>&nbsp;\u3002\u5fc5\u8981\u65f6\u66f4\u6539\u7b26\u53f7\uff08\u4ece\u6b65\u9aa4 2 \u5f00\u59cb\uff09\u3002<\/li><li>\u5982\u679c\u6574\u6570\u6570\u8d85\u8fc7 32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\u8303\u56f4&nbsp;<code>[\u22122<sup>31<\/sup>,&nbsp; 2<sup>31&nbsp;<\/sup>\u2212 1]<\/code>&nbsp;\uff0c\u9700\u8981\u622a\u65ad\u8fd9\u4e2a\u6574\u6570\uff0c\u4f7f\u5176\u4fdd\u6301\u5728\u8fd9\u4e2a\u8303\u56f4\u5185\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c0f\u4e8e&nbsp;<code>\u22122<sup>31<\/sup><\/code>&nbsp;\u7684\u6574\u6570\u5e94\u8be5\u88ab\u56fa\u5b9a\u4e3a&nbsp;<code>\u22122<sup>31<\/sup><\/code>&nbsp;\uff0c\u5927\u4e8e&nbsp;<code>2<sup>31&nbsp;<\/sup>\u2212 1<\/code>&nbsp;\u7684\u6574\u6570\u5e94\u8be5\u88ab\u56fa\u5b9a\u4e3a&nbsp;<code>2<sup>31&nbsp;<\/sup>\u2212 1<\/code>&nbsp;\u3002<\/li><li>\u8fd4\u56de\u6574\u6570\u4f5c\u4e3a\u6700\u7ec8\u7ed3\u679c\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u672c\u9898\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\u53ea\u5305\u62ec\u7a7a\u683c\u5b57\u7b26\u00a0<code>' '<\/code>\u00a0\u3002<\/li><li>\u9664\u524d\u5bfc\u7a7a\u683c\u6216\u6570\u5b57\u540e\u7684\u5176\u4f59\u5b57\u7b26\u4e32\u5916\uff0c<strong>\u8bf7\u52ff\u5ffd\u7565<\/strong>\u00a0\u4efb\u4f55\u5176\u4ed6\u5b57\u7b26\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>s = \"42\"\n<strong>\u8f93\u51fa\uff1a<\/strong>42\n<strong>\u89e3\u91ca\uff1a<\/strong>\u52a0\u7c97\u7684\u5b57\u7b26\u4e32\u4e3a\u5df2\u7ecf\u8bfb\u5165\u7684\u5b57\u7b26\uff0c\u63d2\u5165\u7b26\u53f7\u662f\u5f53\u524d\u8bfb\u53d6\u7684\u5b57\u7b26\u3002\n\u7b2c 1 \u6b65\uff1a\"42\"\uff08\u5f53\u524d\u6ca1\u6709\u8bfb\u5165\u5b57\u7b26\uff0c\u56e0\u4e3a\u6ca1\u6709\u524d\u5bfc\u7a7a\u683c\uff09\n         ^\n\u7b2c 2 \u6b65\uff1a\"42\"\uff08\u5f53\u524d\u6ca1\u6709\u8bfb\u5165\u5b57\u7b26\uff0c\u56e0\u4e3a\u8fd9\u91cc\u4e0d\u5b58\u5728 '-' \u6216\u8005 '+'\uff09\n         ^\n\u7b2c 3 \u6b65\uff1a\"<strong>42<\/strong>\"\uff08\u8bfb\u5165 \"42\"\uff09\n           ^\n\u89e3\u6790\u5f97\u5230\u6574\u6570 42 \u3002\n\u7531\u4e8e \"42\" \u5728\u8303\u56f4 &#91;-2<sup>31<\/sup>, 2<sup>31<\/sup> - 1] \u5185\uff0c\u6700\u7ec8\u7ed3\u679c\u4e3a 42 \u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>s = \"   -42\"\n<strong>\u8f93\u51fa\uff1a<\/strong>-42\n<strong>\u89e3\u91ca\uff1a<\/strong>\n\u7b2c 1 \u6b65\uff1a\"-42\"\uff08\u8bfb\u5165\u524d\u5bfc\u7a7a\u683c\uff0c\u4f46\u5ffd\u89c6\u6389\uff09\n            ^\n\u7b2c 2 \u6b65\uff1a\"   <strong>-<\/strong>42\"\uff08\u8bfb\u5165 '-' \u5b57\u7b26\uff0c\u6240\u4ee5\u7ed3\u679c\u5e94\u8be5\u662f\u8d1f\u6570\uff09\n             ^\n\u7b2c 3 \u6b65\uff1a\"   -<strong>42<\/strong>\"\uff08\u8bfb\u5165 \"42\"\uff09\n               ^\n\u89e3\u6790\u5f97\u5230\u6574\u6570 -42 \u3002\n\u7531\u4e8e \"-42\" \u5728\u8303\u56f4 &#91;-2<sup>31<\/sup>, 2<sup>31<\/sup> - 1] \u5185\uff0c\u6700\u7ec8\u7ed3\u679c\u4e3a -42 \u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>s = \"4193 with words\"\n<strong>\u8f93\u51fa\uff1a<\/strong>4193\n<strong>\u89e3\u91ca\uff1a<\/strong>\n\u7b2c 1 \u6b65\uff1a\"4193 with words\"\uff08\u5f53\u524d\u6ca1\u6709\u8bfb\u5165\u5b57\u7b26\uff0c\u56e0\u4e3a\u6ca1\u6709\u524d\u5bfc\u7a7a\u683c\uff09\n         ^\n\u7b2c 2 \u6b65\uff1a\"4193 with words\"\uff08\u5f53\u524d\u6ca1\u6709\u8bfb\u5165\u5b57\u7b26\uff0c\u56e0\u4e3a\u8fd9\u91cc\u4e0d\u5b58\u5728 '-' \u6216\u8005 '+'\uff09\n         ^\n\u7b2c 3 \u6b65\uff1a\"<strong>4193<\/strong> with words\"\uff08\u8bfb\u5165 \"4193\"\uff1b\u7531\u4e8e\u4e0b\u4e00\u4e2a\u5b57\u7b26\u4e0d\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u6240\u4ee5\u8bfb\u5165\u505c\u6b62\uff09\n             ^\n\u89e3\u6790\u5f97\u5230\u6574\u6570 4193 \u3002\n\u7531\u4e8e \"4193\" \u5728\u8303\u56f4 &#91;-2<sup>31<\/sup>, 2<sup>31<\/sup> - 1] \u5185\uff0c\u6700\u7ec8\u7ed3\u679c\u4e3a 4193 \u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;4\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>s = \"words and 987\"\n<strong>\u8f93\u51fa\uff1a<\/strong>0\n<strong>\u89e3\u91ca\uff1a<\/strong>\n\u7b2c 1 \u6b65\uff1a\"words and 987\"\uff08\u5f53\u524d\u6ca1\u6709\u8bfb\u5165\u5b57\u7b26\uff0c\u56e0\u4e3a\u6ca1\u6709\u524d\u5bfc\u7a7a\u683c\uff09\n         ^\n\u7b2c 2 \u6b65\uff1a\"words and 987\"\uff08\u5f53\u524d\u6ca1\u6709\u8bfb\u5165\u5b57\u7b26\uff0c\u56e0\u4e3a\u8fd9\u91cc\u4e0d\u5b58\u5728 '-' \u6216\u8005 '+'\uff09\n         ^\n\u7b2c 3 \u6b65\uff1a\"words and 987\"\uff08\u7531\u4e8e\u5f53\u524d\u5b57\u7b26 'w' \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u6240\u4ee5\u8bfb\u5165\u505c\u6b62\uff09\n         ^\n\u89e3\u6790\u5f97\u5230\u6574\u6570 0 \uff0c\u56e0\u4e3a\u6ca1\u6709\u8bfb\u5165\u4efb\u4f55\u6570\u5b57\u3002\n\u7531\u4e8e 0 \u5728\u8303\u56f4 &#91;-2<sup>31<\/sup>, 2<sup>31<\/sup> - 1] \u5185\uff0c\u6700\u7ec8\u7ed3\u679c\u4e3a 0 \u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>s = \"-91283472332\"\n<strong>\u8f93\u51fa\uff1a<\/strong>-2147483648\n<strong>\u89e3\u91ca\uff1a<\/strong>\n\u7b2c 1 \u6b65\uff1a\"-91283472332\"\uff08\u5f53\u524d\u6ca1\u6709\u8bfb\u5165\u5b57\u7b26\uff0c\u56e0\u4e3a\u6ca1\u6709\u524d\u5bfc\u7a7a\u683c\uff09\n         ^\n\u7b2c 2 \u6b65\uff1a\"<strong>-<\/strong>91283472332\"\uff08\u8bfb\u5165 '-' \u5b57\u7b26\uff0c\u6240\u4ee5\u7ed3\u679c\u5e94\u8be5\u662f\u8d1f\u6570\uff09\n          ^\n\u7b2c 3 \u6b65\uff1a\"-<strong>91283472332<\/strong>\"\uff08\u8bfb\u5165 \"91283472332\"\uff09\n                     ^\n\u89e3\u6790\u5f97\u5230\u6574\u6570 -91283472332 \u3002\n\u7531\u4e8e -91283472332 \u5c0f\u4e8e\u8303\u56f4 &#91;-2<sup>31<\/sup>, 2<sup>31<\/sup> - 1] \u7684\u4e0b\u754c\uff0c\u6700\u7ec8\u7ed3\u679c\u88ab\u622a\u65ad\u4e3a -2<sup>31<\/sup> = -2147483648 \u3002<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>0 &lt;= s.length &lt;= 200<\/code><\/li><li><code>s<\/code>&nbsp;\u7531\u82f1\u6587\u5b57\u6bcd\uff08\u5927\u5199\u548c\u5c0f\u5199\uff09\u3001\u6570\u5b57\uff08<code>0-9<\/code>\uff09\u3001<code>' '<\/code>\u3001<code>'+'<\/code>\u3001<code>'-'<\/code>&nbsp;\u548c&nbsp;<code>'.'<\/code>&nbsp;\u7ec4\u6210<\/li><\/ul>\n\n\n\n<p>\u7b2c\u4e00\u6b21\u5c1d\u8bd5\uff1a\u6839\u636e\u9898\u76ee\u8981\u6c42\uff0c\u5c31\u786c\u5199\u5c31\u5b8c\u4e86\uff0c\u8003\u8651\u5404\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\r\nclass Solution:\r\n    def myAtoi(self, s: str) -> int:\r\n        s=s.lstrip()+\" \"\r\n        j=0\r\n        if s&#91;0]==\"-\" and s&#91;1] in  &#91;\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]: \r\n           for i in s&#91;1:]:\r\n               re=\"-\"\r\n               j=j+1\r\n               while i not in &#91;\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"] or i==\".\":\r\n                    return int(s&#91;0:j]) if int(s&#91;0:j])>-2**31 else -2**31\r\n        elif s&#91;0] ==\"+\" and s&#91;1] in  &#91;\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]:\r\n            for i in s&#91;1:]:\r\n               j=j+1\r\n               while i not in &#91;\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]or i==\".\":\r\n                    return int(s&#91;1:j])  if int(s&#91;1:j])&lt;2**31-1 else 2**31-1\r\n        elif s&#91;0] in  &#91;\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]:\r\n            for i in s&#91;1:]:\r\n               j=j+1\r\n               while i not in &#91;\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]or i==\".\":\r\n                    return int(s&#91;0:j])  if int(s&#91;0:j])&lt;2**31-1 else 2**31-1\r\n        else:return 0 \r\n            \r\n# @lc code=end\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"701\" height=\"210\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-168.png\" alt=\"\" class=\"wp-image-1733\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-168.png 701w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-168-300x90.png 300w\" sizes=\"(max-width: 701px) 100vw, 701px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u4f60\u6765\u5b9e\u73b0\u4e00\u4e2a&nbsp;myAtoi(string s)&nbsp;\u51fd\u6570\uff0c\u4f7f\u5176\u80fd\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e00\u4e2a 32 \u4f4d &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/10\/leetcodeday8\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday8 &#8211;\u5b57\u7b26\u4e32\u8f6c\u6362\u6574\u6570 (atoi)<\/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\/1732"}],"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=1732"}],"version-history":[{"count":1,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1732\/revisions"}],"predecessor-version":[{"id":1734,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1732\/revisions\/1734"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1732"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}