Reverse Number

Reverse Number without extra space

class Solution {
public:
    bool isPalindrome(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (x < 0) {
            return false;
        }
        int y = x, w;
        for (w = 1; y > 9; y /= 10, w *= 10)
        ;
        for (;w > 1; w /= 100) { //去掉2位所有 w/=100
            if (x % 10 != x / w) {
                return false;
            }
            x %= w; //去掉最高位
            x /= 10; //去掉最低位
            
        }
        return true;
        
    }
};

 

posted @ 2013-09-09 22:36  一只会思考的猪  阅读(172)  评论(0编辑  收藏  举报