Palindrome Number [LeetCode]

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

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

Palindrome Number [LeetCode]
    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;
    }
Palindrome Number [LeetCode]

Palindrome Number [LeetCode]

上一篇:static 变量


下一篇:POJ 1637 Sightseeing tour (混合图欧拉路判定)