{"id":2156,"date":"2016-09-01T21:55:07","date_gmt":"2016-09-01T13:55:07","guid":{"rendered":"http:\/\/boweihe.me\/?p=2156"},"modified":"2016-09-01T21:55:07","modified_gmt":"2016-09-01T13:55:07","slug":"leetcode-7-reverse-integer","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2156","title":{"rendered":"LeetCode 7. Reverse Integer"},"content":{"rendered":"<p>Reverse digits of an integer.<br \/>\n<b>Example1:<\/b> x = 123, return 321<br \/>\n<b>Example2:<\/b> x = -123, return -321<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u68c0\u6d4b\u6ea2\u51fa\u8981\u5728\u6ea2\u51fa\u4e4b\u524d\uff0c\u56e0\u4e3a\u6ea2\u51fa\u7684\u7ed3\u679c\u662fundefined\u7684\u3002<br \/>\n\u6bd4\u5982\u8981\u5224\u65ad\u52a0\u6cd5\u6ea2\u51fa\uff0c\u60f3\u5f53\u7136\u7684\u5c31\u662f A + B &gt; INT_MAX\uff0c\u4f46\u662f\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0cA + B\u6307\u4e0d\u5b9a\u52a0\u51fa\u4ec0\u4e48\u4e1c\u897f\u6765\uff0c\u5e94\u5f53\u662f A &gt; INT_MAX &#8211; B.<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true  \">class Solution {\npublic:\n    bool safeMultiply10(int&amp; result){\n        \/\/Multiply result by 10 if not overflow\n        \/\/Return false if it overflows\n        if(result &gt; 0){\n            if(result &gt; INT_MAX \/ 10)\n                return false;\n        } else if(result &lt; 0) {\n            if(result &lt; INT_MIN \/ 10)\n                return false;\n        }\n        result *= 10;\n        return true;\n    }\n    bool safePlus(int&amp; result, int r){\n        \/\/Return false if it overflows\n        if(result &gt; 0){\n            if(result &gt; INT_MAX - r)\n                return false;\n        } else if (result &lt; 0) {\n            if(result &lt; INT_MIN - r)\n                return false;\n        }\n        result += r;\n        return true;\n    }\n    int reverse(int x) {\n        int result = 0;\n        \/\/bool negFlag = x&lt;0;\n        while(x != 0){\n            int r = x % 10;\n            if(!safeMultiply10(result))\n                return 0;\n            if(!safePlus(result, r))\n                return 0; \/\/overflow\n            x \/= 10;\n        }\n        return result;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 \u601d\u8def \u68c0\u6d4b\u6ea2\u51fa\u8981\u5728\u6ea2\u51fa\u4e4b\u524d\uff0c\u56e0\u4e3a\u6ea2\u51fa\u7684\u7ed3\u679c\u662fundefined\u7684\u3002 \u6bd4\u5982\u8981\u5224\u65ad\u52a0\u6cd5\u6ea2\u51fa\uff0c\u60f3\u5f53\u7136\u7684\u5c31\u662f A + B &gt; INT_MAX\uff0c\u4f46\u662f\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0cA + B\u6307\u4e0d\u5b9a\u52a0\u51fa\u4ec0\u4e48\u4e1c\u897f\u6765\uff0c\u5e94\u5f53\u662f A &gt; INT_MAX &#8211; B. \u4ee3\u7801 class Solution { public: bool safeMultiply10(int&amp; result){ \/\/Multiply result by 10 if not overflow \/\/Return false if it overflows if(result &gt; 0){ [&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":[],"class_list":["post-2156","post","type-post","status-publish","format-standard","hentry","category-study"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2156","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=2156"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2156\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}