{"id":3931,"date":"2018-09-12T09:37:00","date_gmt":"2018-09-12T16:37:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3931"},"modified":"2018-09-12T09:37:54","modified_gmt":"2018-09-12T16:37:54","slug":"leetcode-8-string-to-integer-atoi","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-8-string-to-integer-atoi\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 8. String to Integer (atoi)"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Implement\u00a0<code>atoi<\/code>\u00a0which\u00a0converts a string to an integer.<\/p>\n<p>The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.<\/p>\n<p>The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.<\/p>\n<p>If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.<\/p>\n<p>If no valid conversion could be performed, a zero value is returned.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>Only the space character\u00a0<code>' '<\/code>\u00a0is considered as whitespace character.<\/li>\n<li>Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [\u22122<sup>31<\/sup>,\u00a0 2<sup>31\u00a0<\/sup>\u2212 1]. If the numerical value is out of the range of representable values, INT_MAX (2<sup>31\u00a0<\/sup>\u2212 1) or INT_MIN (\u22122<sup>31<\/sup>) is returned.<\/li>\n<\/ul>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"42\"\r\n<strong>Output:<\/strong> 42\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"   -42\"\r\n<strong>Output:<\/strong> -42\r\n<strong>Explanation:<\/strong> The first non-whitespace character is '-', which is the minus sign.\r\n\u00a0            Then take as many numerical digits as possible, which gets 42.\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"4193 with words\"\r\n<strong>Output:<\/strong> 4193\r\n<strong>Explanation:<\/strong> Conversion stops at digit '3' as the next character is not a numerical digit.\r\n<\/pre>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"words and 987\"\r\n<strong>Output:<\/strong> 0\r\n<strong>Explanation:<\/strong> The first non-whitespace character is 'w', which is not a numerical \r\n\u00a0            digit or a +\/- sign. Therefore no valid conversion could be performed.<\/pre>\n<p><strong>Example 5:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"-91283472332\"\r\n<strong>Output:<\/strong> -2147483648\r\n<strong>Explanation:<\/strong> The number \"-91283472332\" is out of the range of a 32-bit signed integer.\r\n\u00a0            Thefore INT_MIN (\u22122<sup>31<\/sup>) is returned.<\/pre>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>You need to handle all corner cases in order to pass&#8230;<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int myAtoi(const string&amp; s) {\r\n    long long ans = 0;\r\n    int neg = 0;\r\n    int pos = 0;\r\n    bool p = false; \/\/ has '.'\r\n    bool a = false; \/\/ has letters\r\n    bool n = false; \/\/ has number\r\n    for (int i = 0; i &lt; s.length(); ++i) {      \r\n      if (s[i] == ' ') { if (n || neg || pos) break; }\r\n      else if (s[i] == '-') { if (n) break; else neg++; }\r\n      else if (s[i] == '+') { if (n) break; else pos++; }\r\n      else if (s[i] == '.') p = true;\r\n      else if (s[i] &gt;= '0' &amp;&amp; s[i] &lt;= '9' &amp;&amp; !p &amp;&amp; !a) {\r\n        ans = ans * 10 + (s[i] - '0');\r\n        n = true;\r\n      } else {\r\n        a = true;\r\n      }\r\n      if (ans \/ 10 &gt; INT_MAX) break; \/\/ ans can be &gt; 64 bit\r\n    }\r\n\r\n    if (neg) ans = -ans;\r\n\r\n    if (ans &gt; INT_MAX) ans = INT_MAX;\r\n    if (ans &lt; INT_MIN) ans = INT_MIN;\r\n\r\n    if (pos + neg &gt; 1) ans = 0;\r\n\r\n    return ans;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Implement\u00a0atoi\u00a0which\u00a0converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[402,93,173,177,403,4],"class_list":["post-3931","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-atoi","tag-conversion","tag-expression","tag-medium","tag-overflow","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3931"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3931\/revisions"}],"predecessor-version":[{"id":3933,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3931\/revisions\/3933"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}