{"id":687,"date":"2020-09-26T11:11:03","date_gmt":"2020-09-26T03:11:03","guid":{"rendered":"http:\/\/39.96.58.60\/?p=687"},"modified":"2022-10-18T16:38:16","modified_gmt":"2022-10-18T08:38:16","slug":"leetcode-8-%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e6%95%b4%e6%95%b0-atoi","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=687","title":{"rendered":"Leetcode 8. \u5b57\u7b26\u4e32\u8f6c\u6362\u6574\u6570 (atoi)"},"content":{"rendered":"<p>\u8bf7\u4f60\u6765\u5b9e\u73b0\u4e00\u4e2a&nbsp;<code>atoi<\/code>&nbsp;\u51fd\u6570\uff0c\u4f7f\u5176\u80fd\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570\u3002<\/p>\n<p>\u9996\u5148\uff0c\u8be5\u51fd\u6570\u4f1a\u6839\u636e\u9700\u8981\u4e22\u5f03\u65e0\u7528\u7684\u5f00\u5934\u7a7a\u683c\u5b57\u7b26\uff0c\u76f4\u5230\u5bfb\u627e\u5230\u7b2c\u4e00\u4e2a\u975e\u7a7a\u683c\u7684\u5b57\u7b26\u4e3a\u6b62\u3002\u63a5\u4e0b\u6765\u7684\u8f6c\u5316\u89c4\u5219\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u4e3a\u6b63\u6216\u8005\u8d1f\u53f7\u65f6\uff0c\u5219\u5c06\u8be5\u7b26\u53f7\u4e0e\u4e4b\u540e\u9762\u5c3d\u53ef\u80fd\u591a\u7684\u8fde\u7eed\u6570\u5b57\u5b57\u7b26\u7ec4\u5408\u8d77\u6765\uff0c\u5f62\u6210\u4e00\u4e2a\u6709\u7b26\u53f7\u6574\u6570\u3002<\/li>\n<li>\u5047\u5982\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u662f\u6570\u5b57\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u4e0e\u4e4b\u540e\u8fde\u7eed\u7684\u6570\u5b57\u5b57\u7b26\u7ec4\u5408\u8d77\u6765\uff0c\u5f62\u6210\u4e00\u4e2a\u6574\u6570\u3002<\/li>\n<li>\u8be5\u5b57\u7b26\u4e32\u5728\u6709\u6548\u7684\u6574\u6570\u90e8\u5206\u4e4b\u540e\u4e5f\u53ef\u80fd\u4f1a\u5b58\u5728\u591a\u4f59\u7684\u5b57\u7b26\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5b57\u7b26\u53ef\u4ee5\u88ab\u5ffd\u7565\uff0c\u5b83\u4eec\u5bf9\u51fd\u6570\u4e0d\u5e94\u8be5\u9020\u6210\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<p>\u6ce8\u610f\uff1a\u5047\u5982\u8be5\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u975e\u7a7a\u683c\u5b57\u7b26\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u6574\u6570\u5b57\u7b26\u3001\u5b57\u7b26\u4e32\u4e3a\u7a7a\u6216\u5b57\u7b26\u4e32\u4ec5\u5305\u542b\u7a7a\u767d\u5b57\u7b26\u65f6\uff0c\u5219\u4f60\u7684\u51fd\u6570\u4e0d\u9700\u8981\u8fdb\u884c\u8f6c\u6362\uff0c\u5373\u65e0\u6cd5\u8fdb\u884c\u6709\u6548\u8f6c\u6362\u3002<\/p>\n<p>\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\uff0c\u82e5\u51fd\u6570\u4e0d\u80fd\u8fdb\u884c\u6709\u6548\u7684\u8f6c\u6362\u65f6\uff0c\u8bf7\u8fd4\u56de 0 \u3002<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u672c\u9898\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\u53ea\u5305\u62ec\u7a7a\u683c\u5b57\u7b26 <code>' '<\/code> \u3002<\/li>\n<li>\u5047\u8bbe\u6211\u4eec\u7684\u73af\u5883\u53ea\u80fd\u5b58\u50a8 32 \u4f4d\u5927\u5c0f\u7684\u6709\u7b26\u53f7\u6574\u6570\uff0c\u90a3\u4e48\u5176\u6570\u503c\u8303\u56f4\u4e3a&nbsp;[\u22122<sup>31<\/sup>,&nbsp; 2<sup>31&nbsp;<\/sup>\u2212 1]\u3002\u5982\u679c\u6570\u503c\u8d85\u8fc7\u8fd9\u4e2a\u8303\u56f4\uff0c\u8bf7\u8fd4\u56de &nbsp;INT_MAX (2<sup>31&nbsp;<\/sup>\u2212 1) \u6216&nbsp;INT_MIN (\u22122<sup>31<\/sup>) \u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \"42\"\n\n<strong>\u8f93\u51fa:<\/strong> 42\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \"   -42\"\n\n<strong>\u8f93\u51fa:<\/strong> -42\n\n<strong>\u89e3\u91ca: <\/strong>\u7b2c\u4e00\u4e2a\u975e\u7a7a\u767d\u5b57\u7b26\u4e3a '-', \u5b83\u662f\u4e00\u4e2a\u8d1f\u53f7\u3002\n\n&nbsp;    \u6211\u4eec\u5c3d\u53ef\u80fd\u5c06\u8d1f\u53f7\u4e0e\u540e\u9762\u6240\u6709\u8fde\u7eed\u51fa\u73b0\u7684\u6570\u5b57\u7ec4\u5408\u8d77\u6765\uff0c\u6700\u540e\u5f97\u5230 -42 \u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;3:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \"4193 with words\"\n\n<strong>\u8f93\u51fa:<\/strong> 4193\n\n<strong>\u89e3\u91ca:<\/strong> \u8f6c\u6362\u622a\u6b62\u4e8e\u6570\u5b57 '3' \uff0c\u56e0\u4e3a\u5b83\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u4e0d\u4e3a\u6570\u5b57\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;4:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \"words and 987\"\n\n<strong>\u8f93\u51fa:<\/strong> 0\n\n<strong>\u89e3\u91ca:<\/strong> \u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u662f 'w', \u4f46\u5b83\u4e0d\u662f\u6570\u5b57\u6216\u6b63\u3001\u8d1f\u53f7\u3002\n\n     \u56e0\u6b64\u65e0\u6cd5\u6267\u884c\u6709\u6548\u7684\u8f6c\u6362\u3002<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;5:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \"-91283472332\"\n\n<strong>\u8f93\u51fa:<\/strong> -2147483648\n\n<strong>\u89e3\u91ca:<\/strong> \u6570\u5b57 \"-91283472332\" \u8d85\u8fc7 32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\u8303\u56f4\u3002 \n\n&nbsp;    \u56e0\u6b64\u8fd4\u56de INT_MIN (\u22122<sup>31<\/sup>) \u3002\n\n<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6570\u5b66\u3001 \u5b57\u7b26\u4e32\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a52 ms\n\u5185\u5b58\u6d88\u8017\uff1a13.6 MB\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u8fd9\u79bb\u4f7f\u7528\u7684\u662f\u5b98\u65b9\u63d0\u4f9b\u7684 \u81ea\u52a8\u673a \u601d\u60f3\u3002\n    \u9996\u5148\uff0c\u5206\u6790\u5404\u5b57\u7b26\u5bf9\u5e94\u7684\u72b6\u6001\uff1a\n                        ' '     +-      numbel      other\n            start       start   signed  innumbel    end\n            signed      end     end     innumbel    end\n            innumbel    end     end     innumbel    end\n            end         end     end     end         end\n    \u521d\u59cb\u72b6\u6001\u4e3a start,\n    \u5728start\u72b6\u6001\u4e0b\uff0c \u82e5\u9996\u5b57\u7b26\u4e3a' '\uff0c\u5219\u72b6\u6001\u4e0d\u53d8\u3002\u82e5\u9996\u5b57\u7b26\u4e3a+-\uff0c\u5219\u5c06\u72b6\u6001\u8c03\u4e3a signed\u3002 \u82e5\u9996\u5b57\u7b26\u662f\u6570\u5b57\uff0c\u5219\u72b6\u6001\u8c03\u6574\u4e3a innumbel\uff0c \u82e5\u662f\u5176\u4ed6\u5b57\u7b26\uff0c\u5219\u8c03\u4e3a end.\n    \u5728signed\u72b6\u6001\u4e0b\uff0c\u82e5\u5b57\u7b26\u4e3a\u6570\u5b57\uff0c\u5219\u5c06\u72b6\u6001\u8c03\u6574\u4e3ainnumbel\uff0c \u5426\u5219\u4e3aend\n    \u5728innumbel\u72b6\u6001\u4e0b\uff0c\u82e5\u5b57\u7b26\u4e3a\u6570\u5b57\uff0c\u5219\u4fdd\u6301innumbel\u72b6\u6001\uff0c\u5426\u5219\u4e3aend\n\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def myAtoi(self, str: str) -&gt; int:\n        am = AM()\n        for c in str:\n            if am.state == 'end':\n                break\n            am.get(c)\n        return am.sign * am.num\n\n\nclass AM:\n    def __init__(self):\n        self.tabel = {\n            'start': ['start', 'signed', 'innumbel', 'end'],\n            'signed': ['end', 'end', 'innumbel', 'end'],\n            'innumbel': ['end', 'end', 'innumbel', 'end'],\n            'end': ['end', 'end', 'end', 'end'],\n        }                       # \u72b6\u6001\u8868\n        self.sign = 1           # \u521d\u59cb\u7b26\u53f7\u4e3a\u6b63\n        self.num = 0            # \u7528\u4e8e\u5b58\u50a8\u8f6c\u6362\u540e\u7684\u6570\u5b57\n        self.state = 'start'    # \u521d\u59cb\u72b6\u6001\u4e3astart\n        self.max_ = 2**31 - 1   # \u6700\u5927\u6570\u503c\u9650\u5236\uff0c\u8fd9\u91cc\u521d\u59cb\u9650\u5236\u4e3a 2**31-1 \uff08\u6b63\u6570\uff09\n\n    def get_col(self, c:str) -&gt; int:    # \u9996\u5b57\u7b26\u5224\u65ad\uff0c\u8fd4\u56de\u4e3a\u72b6\u6001\u8868\u5217\u7684\u7d22\u5f15\n        if c.isdigit():\n            return 2    # innumbel\n        if c.isspace():\n            return 0    # start\n        if c == '+' or c == '-':\n            return 1    # signed\n        else:\n            return 3    # end\n\n    def get(self, c):\n        self.state = self.tabel[self.state][self.get_col(c)]\n        if self.state == 'start':   # start \u4e0d\u64cd\u4f5c\uff0c\u7ee7\u7eed\u4e0b\u4e00\u4e2a\u5b57\u7b26\n            pass\n        if self.state == 'signed':  # signed \u5c06\u72b6\u6001\u8c03\u6574\u4e3asigned\uff0c\u4e14\u5bf9\u521d\u59cb\u7b26\u53f7\u4e0e\u6700\u5927\u6570\u503c\u8fdb\u884c\u8c03\u6574\n            if c == '+':\n                self.sign = 1\n            else:\n                self.sign = -1\n                self.max_ = self.max_ + 1   # \u8d1f\u6570\u65f6\uff0c2**31\n\n        if self.state == 'innumbel':    # innumbel \u72b6\u6001\u65f6\uff0c\u8ba1\u7b97\u6570\u503c\u4e14\u7d2f\u52a0\n            self.num = self.num *10 + int(c)\n            if self.num &gt; self.max_:    # \u82e5\u8d85\u51fa\u6570\u503c\u9650\u5236\uff0c\u5219\u5c06\u72b6\u6001\u7f6e\u4e3aend\n                self.num = self.max_\n                self.state = 'end'\n\n        if self.state == 'end':     # end \u4e0d\u505a\u64cd\u4f5c\n            pass\n\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u4f60\u6765\u5b9e\u73b0\u4e00\u4e2a&nbsp;atoi&nbsp;\u51fd\u6570\uff0c\u4f7f\u5176\u80fd\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570\u3002 \u9996\u5148\uff0c\u8be5\u51fd\u6570\u4f1a\u6839\u636e\u9700\u8981\u4e22\u5f03\u65e0\u7528&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/687"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=687"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/687\/revisions"}],"predecessor-version":[{"id":688,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/687\/revisions\/688"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=687"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}