【Leetcode】Palindrome Number

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

思路:若使用【Leetcode】Reverse Integer 的方法。推断反转后的整数是否与原整数同样,则可能出现溢出情况;又由于题目要求不适用额外空间,能够之间对照整数第一个与最后一个数的值。再依次类推。

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0)   return false;
        
        int bitNum = 0;
        int temp = x;
        while(temp != 0)
        {
            temp /= 10;
            bitNum++;
        }
        
        for(int i = 1; i <= bitNum / 2; i++)
        {
            if((x / (int)pow(10, bitNum - i)) % 10 == (x % (int)pow(10, i)) / (int)pow(10, i - 1))
                continue;
            else
                return false;
        }
        
        return true;
    }
};

版权声明:本文博主原创文章,博客,未经同意不得转载。

posted @ 2015-09-26 11:30  lcchuguo  阅读(147)  评论(0编辑  收藏  举报