Accepted C++ simple solution ~ 138 ms


  • 0
    D
    class Solution {
    public:
    
        bool isPalindrome(int x) {
            
            // negative numbers have a '-' sign in front
            // and also multiples of 10 (10, 30, 40, 100, etc..)
            // are not palindromes 
            if(x < 0 || (x!=0 && (x%10 ==0)))
                return false;
                
            // convert integer to string to access
            // digits more easily
            string c = std::to_string(x);
            
            size_t len = c.size();
            unsigned int M = len/2;
            
            for(int i=0; i < M; i++){
                
                if( c[i] != c[len - 1 - i])
                    return false;
            }
            
            return true;
        }
    };

Log in to reply
 

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