{"id":1945,"date":"2022-05-04T14:53:29","date_gmt":"2022-05-04T14:53:29","guid":{"rendered":"https:\/\/www.zttofficial.com\/?p=1945"},"modified":"2022-05-10T15:51:13","modified_gmt":"2022-05-10T15:51:13","slug":"%e5%81%9a%e9%a1%8c%e7%ad%86%e8%a8%98%ef%bc%9apalindrome-number-java","status":"publish","type":"post","link":"https:\/\/www.zttofficial.com\/?p=1945","title":{"rendered":"\u505a\u984c\u7b46\u8a18\uff1aPalindrome Number (Java)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>\u984c\u76ee\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7d66\u5b9a\u4e00\u500b\u6574\u6578 <kbd>x<\/kbd>\uff0c\u5982\u679c <kbd>x<\/kbd> \u662f\u56de\u6587\u6578\uff0c\u5247\u8fd4\u56de <kbd>true<\/kbd>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56de\u6587\u6578\u6307\u4e00\u500b\u6574\u6578\u5411\u5f8c\u8b80\u548c\u5411\u524d\u8b80\u5185\u5bb9\u76f8\u540c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: x = 121\nOutput: true\nExplanation: 121 reads as 121 from left to right and from right to left.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: x = -121\nOutput: false\nExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: x = 10\nOutput: false\nExplanation: Reads 01 from right to left. Therefore it is not a palindrome.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7b97\u8853\u89e3\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5229\u7528 Java \u6574\u6578\u5012\u5e8f\u8a08\u7b97\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java line-numbers\">class Solution {\n    public boolean isPalindrome(int x) {\n        if (x &lt; 0) {\n            return false;\n        }\n        \n        int ori = x;\n        int p = 0;\n        while (ori != 0) {\n            p = p * 10 + ori % 10;\n            ori \/= 10;\n        }\n        \n        return x == p;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">9 ms\uff0c\u8d85\u904e 87.67% \u7684\u7b54\u6848\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f49\u63db\u70ba String \u7684\u89e3\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9032\u968e\u8981\u6c42 Follow Up \u7adf\u7136\u554f\u4e86\u4e00\u53e5 <kbd>Could you solve it without converting the integer to a string? <\/kbd>\u539f\u4f86\u6211\u60f3\u5230\u7684\u5df2\u7d93\u662f\u9032\u968e\u65b9\u6cd5\u4e86\uff0c\u5e38\u898f\u89e3\u6cd5\u662f\u8f49\u63db\u70ba String\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java line-numbers\">class Solution {\n     public boolean isPalindrome(int x) {\n         char[] nums = String.valueOf(x).toCharArray();\n         int start = 0;\n         int end = nums.length-1;\n         while(start &lt; end) {\n             if(nums[start] != nums[end]) return false;\n             start++; end--;\n         }\n        return true;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">19 ms\uff0c\u8d85\u904e 19.48% \u7684\u7b54\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u984c\u76ee\u63cf\u8ff0\uff1a \u7d66\u5b9a\u4e00\u500b\u6574\u6578 x\uff0c\u5982\u679c x \u662f\u56de\u6587\u6578\uff0c\u5247\u8fd4\u56de true\u3002 \u56de\u6587\u6578\u6307\u4e00\u500b\u6574\u6578\u5411\u5f8c\u8b80\u548c\u5411\u524d\u8b80\u5185\u5bb9\u76f8\u540c\u3002 Example 1: Example 2: Example 3: \u7b97\u8853\u89e3\u6cd5\uff1a \u5229\u7528 Java \u6574\u6578\u5012\u5e8f\u8a08\u7b97\uff1a 9 ms\uff0c\u8d85\u904e 87.67% \u7684\u7b54\u6848\u3002 \u8f49\u63db\u70ba String \u7684\u89e3\u6cd5\uff1a \u9032\u968e\u8981\u6c42 Follow Up \u7adf\u7136\u554f\u4e86\u4e00\u53e5 Could you solve it without converting the integer to a string? \u539f\u4f86\u6211\u60f3\u5230\u7684\u5df2\u7d93\u662f\u9032\u968e\u65b9\u6cd5\u4e86\uff0c\u5e38\u898f\u89e3\u6cd5\u662f\u8f49\u63db\u70ba String\uff1a 19 ms\uff0c\u8d85\u904e 19.48% \u7684\u7b54\u6848\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,135],"tags":[137],"class_list":["post-1945","post","type-post","status-publish","format-standard","hentry","category-all","category-135","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/1945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1945"}],"version-history":[{"count":5,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/1945\/revisions"}],"predecessor-version":[{"id":1960,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/1945\/revisions\/1960"}],"wp:attachment":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}