Easy C++ solution


  • 2
    W

    bool isPalindrome(int x) {

        if( x < 0 ) return false;
        
        int result = 0;
        int num = x;
        
        while( num > 0 )
        {
            result = result * 10 + num % 10;
            
            if( result > x ) return false;
            
            num = num / 10;
        }
        
        if( result == x ) return true;
        
        else return false;
    }

  • 0
    H
    bool isPalindrome(int x) {
    if(x < 10)
        return x >= 0;
    if(x%10 ==0)
        return false;
    int y=0;
    while(x > y && x >9)
    {
        if(x/10 == y) return true;
        y = y*10 + (x%10);
        x /= 10;
    }
    return x==y;
    

    }


  • 0
    H

    This approach is simple and intuitive, but could overflow, for example:
    214748364


  • 0
    W

    Thanks for your reply, I will correct this error . Thank you


Log in to reply
 

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