<LeetCode OJ> 9. Palindrome Number

来源:互联网 发布:下载和目软件 编辑:程序博客网 时间:2024/06/05 20:02

9. Palindrome Number

Total Accepted: 95908 Total Submissions: 319671 Difficulty: Easy

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


分析:

既然不能申请额外的空间,那么最好就原地判断。

1,首先,负数肯定不是回文数字
2,从x的低位一位一位的取出来形成新数的高位
3,最后看是否与原数相等

class Solution {public:    bool isPalindrome(int x) {        if(x<0)//负数            return false;        int key=x;        int ans=0;        while(key)//反转        {            ans*=10;            ans+=key%10;            key=key/10;        }        if(ans==x)//是否相等             return true;        else            return false;    }};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50365751

原作者博客:http://blog.csdn.net/ebowtang

1 0