LeetCode 9. Palindrome Number

https://leetcode.com/problems/palindrome-number/

试了几种办法,这个应该是比较快的。

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0)
            return false;
        int fuck=log10(x);
        int num=0,mid=(fuck)>>1;
        for(int i=0;i<=mid;i++)
            num*=10,num+=x%10,x/=10;
        if(num==x||num/10==x)
            return true;
        return false;
    }
};

 

 附上瞎搞解法

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0)
            return false;
        if(x<10)
            return true;
        int fuck=log10(x);
        int bit=1000000;
        int cnt=6;
        while(cnt!=fuck)
        {
            if(cnt>fuck)
                cnt--,bit/=10;
            else 
                cnt++,bit*=10;
        }
        for(int i=0;i<(fuck+1)>>1;i++)
        {
            if(x%10==x/bit%10)
                x/=10,bit/=100;
            else
                return false;
        }
        return true;

    }
};

 

posted @ 2016-12-09 18:07  Luke_Ye  阅读(142)  评论(0编辑  收藏  举报