首页 > 其他 > 详细

palindrome-number

时间:2017-03-07 08:54:27      阅读:196      评论:0      收藏:0      [点我收藏+]

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

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

 

palindrome-number

原文:http://www.cnblogs.com/xiuxiu55/p/6512815.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!