9. Palindrome Number

来源:互联网 发布:win10网络设置在哪 编辑:程序博客网 时间:2024/06/05 19:24

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

思路:如何取到一个数字第一位和最后一位,判断完后再取到中间的数字。
注意pow的结果是double,要转成int。

class Solution {public:    bool isPalindrome(int x) {        if(x<0)        {            return false;        }        int count = 0;        int y = x;        while(y > 0)        {            y = y/10;            count++;        }        while(count > 1)        {            if(x%10 != x/(int)(pow(10,count-1)))            {                return false;            }            x = (int)(x/10);            x = (int)(x) % (int)(pow(10, count-2));            count-=2;        }        return true;    }};

还有更简洁的方法: 把count转化成10^count做一个base就可以了。

http://www.cnblogs.com/remlostime/archive/2012/11/13/2767676.html

0 0
原创粉丝点击