{"id":3934,"date":"2018-09-12T18:48:10","date_gmt":"2018-09-13T01:48:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3934"},"modified":"2018-09-12T18:54:44","modified_gmt":"2018-09-13T01:54:44","slug":"leetcode-9-palindrome-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-9-palindrome-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 9. Palindrome Number"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Determine whether an integer is a palindrome. An integer\u00a0is\u00a0a\u00a0palindrome when it\u00a0reads the same backward as forward.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> 121\r\n<strong>Output:<\/strong> true\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> -121\r\n<strong>Output:<\/strong> false\r\n<strong>Explanation:<\/strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> 10\r\n<strong>Output:<\/strong> false\r\n<strong>Explanation:<\/strong> Reads 01 from right to left. Therefore it is not a palindrome.\r\n<\/pre>\n<p><strong>Follow up:<\/strong><\/p>\n<p>Could you solve\u00a0it without converting the integer to a string?<\/p>\n<h1><strong>Solution 1: Convert to string (cheating)<\/strong><\/h1>\n<p>Time complexity: O(log10(x))<\/p>\n<p>Space complexity: O(log10(x))<\/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  bool isPalindrome(int x) {\r\n    string s = to_string(x);    \r\n    return s == string(rbegin(s), rend(s));\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: Digit by Digit<\/strong><\/h1>\n<p>Every time we compare the first and last digits of x, if they are not the same, return false. Otherwise, remove first and last digit and continue this process.<\/p>\n<p>How can we achieve that via int math?<\/p>\n<p>e.g. x = 9999, t = pow((10, int)log10(x)) = 1000<\/p>\n<p>first digit: x \/ t, last digit: x % 10<\/p>\n<p>then x = (x &#8211; x \/ t * t) \/ 10 removes first and last digits.<\/p>\n<p>t \/= 100 since we removed two digits.<\/p>\n<p>x \/ t = 9 = 9 = x % 10, 9999 =&gt; 99<\/p>\n<p>9 = 9, 99 =&gt; &#8220;&#8221;<\/p>\n<p>Time complexity: O(log10(x) \/ 2)<\/p>\n<p>Space complexity: O(1)<\/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  bool isPalindrome(int x) {\r\n    if (x &lt; 0) return false;\r\n    int d = static_cast&lt;int&gt;(log10(x) + 1);\r\n    int t = pow(10, d - 1);\r\n    for (int i = 0; i &lt; d \/ 2; ++i) {     \r\n      if (x \/ t != x % 10) return false;\r\n      x = (x - x \/ t * t) \/ 10;\r\n      t \/= 100;     \r\n    }\r\n    return true;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Determine whether an integer is a palindrome. An integer\u00a0is\u00a0a\u00a0palindrome when it\u00a0reads the same backward as forward. Example 1: Input: 121 Output: true Example 2:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,31,95],"class_list":["post-3934","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-math","tag-palindrome","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3934","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=3934"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3934\/revisions"}],"predecessor-version":[{"id":3938,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3934\/revisions\/3938"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}