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;
    
}
}

 

posted on 2016-09-12 03:44  Machelsky  阅读(109)  评论(0)    收藏  举报