标签:
Determine whether an integer is a palindrome. Do this without extra space.
Solution1:
思路:按题目的意思就是负数不能是palindrome。用reverse integer函数判断即可。
public class Solution { public boolean isPalindrome(int x) { if(x<0) { return false; } if(x==reverse(x)) { return true; } return false; } public int reverse(int x) { int sign=(x>=0)?1:-1; x=Math.abs(x); int res=0; while(x>0) { if(res>Integer.MAX_VALUE/10) { return 0; } res=res*10+x%10; x/=10; } return res*sign; } }
标签:
原文地址:http://www.cnblogs.com/Machelsky/p/5863416.html