{"id":1993,"date":"2016-04-19T11:08:15","date_gmt":"2016-04-19T03:08:15","guid":{"rendered":"http:\/\/boweihe.me\/?p=1993"},"modified":"2016-04-19T11:08:15","modified_gmt":"2016-04-19T03:08:15","slug":"8-string-to-integer-atoi","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1993","title":{"rendered":"LeetCode 8. String to Integer (atoi)"},"content":{"rendered":"<h3>\u9898\u76ee<\/h3>\n<p>Implement atoi to convert a string to an integer.<br \/>\n<b>Hint:<\/b> Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.<br \/>\n<b>Notes:<\/b> It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.<br \/>\n<b><span style=\"color: red;\">Update (2015-02-10):<\/span><\/b><br \/>\nThe signature of the <code>C++<\/code> function had been updated. If you still see your function signature accepts a <code>const char *<\/code> argument, please click the reload button to reset your code definition.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u8fd9\u9053\u9898\u6ca1\u4ec0\u4e48\u601d\u8def\uff0c\u4f46\u662f\u8981\u4e3b\u8981<strong>\u4e00\u5b9a\u8981\u5728\u6ea2\u51fa\u4e4b\u524d\u89e3\u51b3\u6ea2\u51fa<\/strong>\uff01\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u5bf9\u6ea2\u51fa\u7684\u5904\u7406\u65b9\u6cd5\u4e0d\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5f97\u5230\u7684\u6ea2\u51fa\u7ed3\u679c\u4e5f\u662f\u4e0d\u540c\u7684\uff0c\u6bd4\u5982int\u7684\u6b63\u6574\u6570\u6ea2\u51fa\u5c31\u662f\u8d1f\u6574\u6570\u4ec0\u4e48\u7684\u5c31\u80af\u5b9a\u4e0d\u5bf9\u3002\u6211\u7684\u60f3\u6cd5\u662f\u7528unsigned int\u628a\u53ef\u80fd\u6ea2\u51fa\u7684\u60c5\u51b5\u515c\u4f4f\u5224\u65ad\uff0c\u53ef\u80fd\u6ea2\u51fa\u7684\u60c5\u51b5\u65e0\u975e\u4e24\u4e2a\uff0c\u4e58\u4ee510\u7684\u65f6\u5019\uff0c\u52a0\u4e00\u4e2a\u4e2a\u4f4d\u6570\u7684\u65f6\u5019\u3002<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    bool willOverflow(unsigned int currNum, char i, bool negFlag){\n        const unsigned int M_INTM = INT_MAX \/ 10;\n        if(currNum &gt; M_INTM){\n            return true;\n        } else if(currNum == M_INTM){\n            if(!negFlag){\n                if(i - '0' &gt; 7) {\n                    return true;\n                }\n            } else{\n                if(i - '0' &gt; 8){\n                    return true;\n                }\n            }\n        }\n        return false;\n    }\n    int myAtoi(string str) {\n        unsigned int retVal = 0;\n        bool negFlag = false;\n        bool overflow = false;\n        if(str.size() &lt; 1)\n            return 0;\n        int i=0;\n        while(str[i] == ' '){\n            i++;\n        }\n        if(str[i] == '+')\n            i++;\n        else if(str[i] == '-'){\n            i++;\n            negFlag = true;\n        }\n        int digCount = 0;\n        while(i &lt; str.size() &amp;&amp; str[i] &gt;= '0' &amp;&amp; str[i] &lt;= '9'){\n            if(digCount &lt; 9 || !willOverflow(retVal, str[i], negFlag)){\n                retVal = retVal * 10 + str[i] - '0';\n            } else {\n                \/\/overflow!\n                overflow = true;\n                break;\n            }\n            i++;\n            digCount++;\n        }\n        if(!overflow){\n            if(negFlag)\n                return -retVal;\n            else\n                return retVal;\n        } else {\n            return negFlag? INT_MIN: INT_MAX;\n        }\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1993","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1993"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1993\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}