57ms C code,a lot of space to improve


  • 0
    S
    int pow10(int n)
    {
        int ret = 1;
        if(n==0) return 1;
        while(n)
        {
            ret = ret*10;
            n--;
        }
        return ret;
    }
    bool isPalindrome(int x) {
        if(x < 0) return false;
        else if(!(x/10)) return true;
        
        int n = 0, i = x;
        while(i)
        {
            i = i / 10;
            n++;
        }
        int even = (n%2==1) ? 0:1;//actually take the operation ~
        n = n/2;
        i = 0;
        while(i<n && x/pow10(i)%10 == x/pow10(2*n-even-i)%10)
        {
            i++;
        }
        if(i>=n) return true;
        else return false;
      
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.