C++ Solution, keep two divs, one for get first digit, one for the last.


  • 4
    C
    class Solution {
    public:
        bool isPalindrome(int x) {
            if(x<0) return false;
            
            int div2 = 1;
            
            while(x/div2 >= 10)
            div2*=10;
            
            int div1 = 1;
            
            while(div1 < div2)
            {
                if((x/div1)%10 != (x/div2)%10) return false;
                
                div1 *= 10;
                div2 /= 10;
            }
            return true;
            
        }
    };

  • 3
    M

    What's the real meaning of using extra space. Does using div1 and div2 mean use extra space?


  • 0
    R

    div1 and div2 is constant space, so doesn't count for extra space. In algorithm realm, extra space means space that proportional to the problem itself.


Log in to reply
 

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