Palindrome Number

和Reverse Integer類似,按位處理,而不要轉換成String。

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0) return false;
        int base = 1;
        while(x/base >= 10) base*=10;
        
        while(x && base)
        {
            int highradix = x/base;
            int lowradix = x%10;
            if(highradix != lowradix)
                return false;
            x %= base;
            x /= 10;
            base /= 100;
        }
        return true;
    }
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章