sourcecode

Sunday, December 9, 2012

Palindrome Number


Palindrome Number
Determine whether an integer is a palindrome. Do this 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 = 0;
        while(y<x){//digit by digit, stack push the lower digit to y
            y = y*10+x%10;
            if (!y) return false;
            x /= 10;
        }
        if (y != x) x = x*10 + y%10;
        if (y == x) return true;
        else return false;
    }
};

No comments: