{"id":2002,"date":"2016-04-22T12:40:27","date_gmt":"2016-04-22T04:40:27","guid":{"rendered":"http:\/\/boweihe.me\/?p=2002"},"modified":"2016-04-22T12:40:27","modified_gmt":"2016-04-22T04:40:27","slug":"leetcode-9-palindrome-number","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2002","title":{"rendered":"LeetCode 9. Palindrome Number"},"content":{"rendered":"<h3>\u9898\u76ee<\/h3>\n<p>Determine whether an integer is a palindrome. Do this without extra space.<\/p>\n<div class=\"spoilers\"><b>Some hints:<\/b>Could negative integers be palindromes? (ie, -1)<br \/>\nIf you are thinking of converting the integer to string, note the restriction of using extra space.<br \/>\nYou could also try reversing an integer. However, if you have solved the problem &#8220;Reverse Integer&#8221;, you know that the reversed integer might overflow. How would you handle such case?<br \/>\nThere is a more generic way of solving this problem.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u9996\u5148\u8981\u6ce8\u610f\uff0c\u8d1f\u6570\u4e0d\u662f\u56de\u6587\u6570\u3002<br \/>\n\u7136\u540e\u9898\u76ee\u4e2d\u8981\u6c42\u4e0d\u80fd\u4f7f\u7528\u989d\u5916\u7684\u7a7a\u95f4\uff0c\u6211\u7684\u7406\u89e3\u662fO(1)\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5373\u4e0d\u80fd\u7528\u5b57\u7b26\u4e32\u6765\u5b58\u50a8?<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<p>\u6211\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u4e0d\u77e5\u9053\u662f\u4e0d\u662f\u8fdd\u89c4\u4e86\uff0c\u5982\u679c\u518d\u82db\u523b\u4e00\u70b9\uff0c\u90a3\u5c31\u662f\u6bcf\u6b21\u6d4b\u8bd5\u5f53\u524d\u6570\u5b57\u7684\u4f4d\u6570\uff08\u8fde\u7eed\u966410\uff09\uff0c\u7136\u540e\u7528\u9664\u6cd5\u548c\u6a2110\u5224\u65ad\u9996\u4f4d\u548c\u672b\u5c3e\u7684\u6570\u5b57\u3002<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    bool isPalindrome(int x) {\n        if(x &lt; 0)\n            return false;\n        int cover = 1;\n        int x_copy = x;\n        while(x_copy\/10 &gt; 0){\n            x_copy \/= 10;\n            cover *= 10;\n        }\n        while(cover &gt;= 10){\n            int front = x\/cover;\n            int end = x%10;\n            if(front != end){\n                return false;\n            }\n            x -= front * cover;\n            x \/= 10;\n            cover \/= 100;\n        }\n        return true;\n    }\n};<\/pre>\n<p>&nbsp;\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee Determine whether an integer is a palindrome. Do this without extra space. Some hints:Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also try reversing an integer. However, if you have solved the problem &#8220;Reverse Integer&#8221;, you [&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-2002","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\/2002","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=2002"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2002\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}