Palindrome Number

来源:互联网 发布:淘宝爆仓是什么意思 编辑:程序博客网 时间:2024/05/21 10:28

Palindrome Number

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

解题思路:

按照回文数的概率,比较对应位置的数字。这个过程比较难想到,具体参照代码!

Code:

class Solution {public:    bool isPalindrome(int x) {        if(x<0)            return false;        int left=1;        int right=10;        int tx=x/10;        while(tx>0)        {            tx/=10;            left*=10;        }                while(left>=right)        {            if(x/left!=x%right/(right/10))                return false;            x%=left;            x=x-x%right;            left/=10;            right*=10;        }        return true;    }};

0 0
原创粉丝点击