palindrome-number
/**
* @author gentleKay
* Determine whether an integer is a palindrome. Do this without extra space.
* click to show spoilers.
* 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 "Reverse Integer",
* you know that the reversed integer might overflow. How would you handle such case?
* There is a more generic way of solving this problem.
*
* 确定整数是否为回文。这样做没有额外的空间。
* 单击以显示扰流器。
* 一些提示:
* 负整数可以是回文吗?(即-1)
* 如果要将整数转换为字符串,请注意使用额外空间的限制。
* 您还可以尝试反转一个整数。但是,如果您解决了“反向整数”问题,
* 您就会知道反向整数可能溢出。你将如何处理这样的案件?
* 有一种更通用的方法来解决这个问题。
*/
/** * @author gentleKay * Determine whether an integer is a palindrome. Do this without extra space. * click to show spoilers. * 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 "Reverse Integer", * you know that the reversed integer might overflow. How would you handle such case? * There is a more generic way of solving this problem. * * 确定整数是否为回文。这样做没有额外的空间。 * 单击以显示扰流器。 * 一些提示: * 负整数可以是回文吗?(即-1) * 如果要将整数转换为字符串,请注意使用额外空间的限制。 * 您还可以尝试反转一个整数。但是,如果您解决了“反向整数”问题, * 您就会知道反向整数可能溢出。你将如何处理这样的案件? * 有一种更通用的方法来解决这个问题。 */ public class Main12 { public static void main(String[] args) { int x = 123421; System.out.println(Main12.isPalindrome(x)); } public static boolean isPalindrome(int x) { if (x < 0) { return false; } int last = 0; int res = x; while (res != 0) { last = last*10 + res%10; res = res / 10; } return last == x; } }