46ms c++ solution o(1)sapce


  • 0
    P
    class Solution {
    public:
        static bool isPalindrome(int x) {
            if(x < 0) return false;
            int total_digits = 0;
            for(int i = x; i > 0; i /= 10){
                total_digits++;
            }
            auto divisor = std::pow(10, total_digits - 1);
            while(x > 0) {
                int a = x / divisor;
                int b = x % 10;
                if(a != b) return false;
                x = (x - a*divisor) / 10;
                divisor /= 100;
            }
            return true;
        }
    };
    
    

  • 0
    S

    @Promer Isn't the code exceeding memory limit for x=2147483647 ?


Log in to reply
 

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