9.Palindrome Number (INT)

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

 

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0) return false; //别忘了负数的情况
        if(x == 0) return true;
        
        int tmp = x/10;
        int pHead = 1;
        int leftDigit, rightDigit, base;
        while(tmp){
            pHead*=10;
            tmp /= 10;
        }
          
        while(pHead >= 1){
            leftDigit = x/pHead;
            rightDigit = x%10;
            if(leftDigit != rightDigit) return false;
            x %= pHead;
            x /= 10;
            pHead /= 100;
        }
        return true;
    }
};

 

posted on 2015-07-22 01:27  joannae  阅读(216)  评论(0编辑  收藏  举报

导航