My 374ms Java Code. (Q: What's the definition of not using extra space?)


  • 0
    H

    Question First:
    I don't know what's the definition of not using extra space. I can handle it without using any Math class function and Class objects but I think it would be crazy if you don't store any primitives like int.

    My code with some extra int stored. (Not so succinct, sorry for that)

    public class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0) {
            return false;
        }
    
        int r = 0; //r for the reverse code.
        int i = 1;
        int copy = x;
        while(x != 0 && i < 10) { //be sure not to overflow.
            r = r * 10 + x % 10;
            x /= 10;
            i++;
        }
        if(i != 10) {
            if(r == copy) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            if(x == x % 10) {
                r = r * 10 + x % 10;
                if(r == copy) {
                    return true;
                }
                else {
                    return false;
                }
            }
            else {
                return false;
            }
        }
    }
    

    }


Log in to reply
 

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