palindrome-number

//判断数组是否是回文 不能用字符串作为辅助,也不能翻转数字(有溢出情况);

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0)
            return false;
        
        if(x <10)
            return true;
        
        int digits = 0;
        int t = x;
        int cnt = 0;
        while(t != 0) 
        {
            t /= 10;
            ++cnt;
        }
        
        int left = pow(10,cnt-1);
        int right = 1;
        
        while(left >= right){
            if(x/left%10 != x/right%10)
                return false;
            
            left /= 10;
            right *= 10;
        }
        return true;
    }
};

 

posted on 2017-03-07 02:00  123_123  阅读(186)  评论(0编辑  收藏  举报