首页 > 其他 > 详细

Palindrome Number [LeetCode]

时间:2014-02-04 01:58:50      阅读:367      评论:0      收藏:0      [点我收藏+]

Determine whether an integer is a palindrome. Do this without extra space.

Summary: First, get the digital number, then caculate digitial from both way.

bubuko.com,布布扣
    int pow_10(int n){
        int result = 1;
        while(n > 0 ){
            result *= 10;
            n --;
        }
        return result;
    }
    bool isPalindrome(int x) {
        if(x < 0)
            return false;
            
        int result = x;
        int digital_num = 0;
        while(result > 0){
            digital_num ++;
            result = result / 10;
        }
        
        if(digital_num <= 1)
            return true;
        
        for(int i = 0; i < digital_num /2; i ++){
            int d1 = 0;
            if(i == 0 ){
                d1 = x / pow_10(digital_num - 1 - i );
            }else{
                d1 = x % pow_10(digital_num - i );
                d1 = d1 / pow_10(digital_num - 1 - i );
            }
            int d2 = x % pow_10(i + 1);
            d2 = d2 / pow_10( i );
            if(d1 != d2)
                return false;
        }    
        return true;
    }
bubuko.com,布布扣

Palindrome Number [LeetCode]

原文:http://www.cnblogs.com/guyufei/p/3537524.html

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