首页 > 其他 > 详细

[LeetCode] Palindrome Number

时间:2014-11-23 11:36:10      阅读:250      评论:0      收藏:0      [点我收藏+]

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

 

因为都额外空间有限制所以不能把他先转成字符串然后用两个指针扫了。负数不算回文数是因为前面多了个负号么=_= 这样的话我只能想到一个办法,就是反转一个数字,同时检查他是否溢出,溢出则返回false,否则对比反转后的数字是否跟原来的数字相等,相等则是回文数。

 

bool isPalindrome(int x) {
    if (x < 0) return false;
    int t = x;
    int v = 0;
    while (t) {
        if (v / 10 > INT_MAX) return false;
        v = v * 10 + (t % 10);
        t /= 10;
    }
    
    if (v == x) return true;
    return false;
}

 

[LeetCode] Palindrome Number

原文:http://www.cnblogs.com/agentgamer/p/4109965.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!