【leetcode】 9. palindrome number

@requires_authorization
@author johnsondu
@create_time 2015.7.13 9:48
@url [palindrome-number](https://leetcode.com/problems/palindrome-number/)
/************************
 *  分离出最左边和最右边的数
 *  然后依次对照就可以
 ***********************/
class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0) return false;
        if(x < 10) return true;

        int base = 1;
        while(x / base >=10) base *= 10;

        while(x)
        {
            int ld = x / base;
            int rd = x % 10;
            if(ld != rd) return false;
            x -= ld * base;
            x /= 10;
            base /= 100;
        }
        return true;
    }
};
posted @ 2018-04-18 10:34  zhchoutai  阅读(125)  评论(0编辑  收藏  举报