码迷,mamicode.com
首页 > 其他好文 > 详细

欢迎使用CSDN-markdown编辑器

时间:2015-07-13 10:18:40      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:leetcode   palindrome   algorithm   

@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;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

欢迎使用CSDN-markdown编辑器

标签:leetcode   palindrome   algorithm   

原文地址:http://blog.csdn.net/zone_programming/article/details/46858727

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!