问题:
package com.nxz.blog.otherTest; public class Test9PalindromeNumber { /** * 判断数字是否是回文 * 方法一:将数字转为字符串,在判断字符串是否是回文数字 * 方法二:之间判断数字,将number % 10 的余数链接后判断是否和源数字相等 */ public boolean isPalindrome(int x) { boolean isPalindrome = true; String s = x + ""; int left = 0, right = s.length() - 1; while (left <= right) { if (s.charAt(left) != s.charAt(right)) { isPalindrome = false; break; } left++; right--; } return isPalindrome; } public boolean isPalindrome2(int x) { if (x < 0 || (x % 10 == 0 && x != 0)) { return false; } int pre = 0; while (x > pre) { pre = pre * 10 + x % 10; x /= 10; } return x == pre || x == pre / 10; } public static void main(String[] args) { Test9PalindromeNumber t = new Test9PalindromeNumber(); boolean palindrome = t.isPalindrome2(12344321); System.out.println(palindrome); } }