C solution 64 ms. What can be improved?


  • 0
    S
    bool isPalindrome(int x) {
        
        int y = x;
        
        int left = 1;
        int right =1;
        
        // negatives are not palindroms
        if ( x < 0 ) return false;
        
        // get power of ten number to get first digit of the number
        while ( y > 9 )
        {            
            y = y /10;
            
            left *= 10;
        }
    
        // digits are palindroms
        if ( left == 1) return true;
     
        right = 1;
        
        // check digits of number in mirror (left-most with rightmost) while middle reached
        while (  left >= right)
        {
            if ( (x/right)%10 != (x/left)%10 ) return false;
            
            right = right * 10;
            
            left = left /10;
        }
     
        return true;
    }

Log in to reply
 

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