javascript implement without extra space


  • 1
    L
    /**
     * @param {number} x
     * @return {boolean}
     */
    var isPalindrome = function (x) {
        var t = x;
        var y = 0;
        var e = 0;
        while (t !== 0) {
            e = t % 10;
            t = (t - e) / 10;
            y *= 10;
            y += e;
        }
        if (x === y)
            return true;
        else
            return false;
    };
    

  • 0
    S

    @ladyrick elegant, but is using the variable 'y' (the reverse of x) considered extra space?


  • 0
    L

    Thank you.
    I think the extra space doesn't include the constant space.
    Actually, if y overflows, x must not be a palindrome number.


Log in to reply
 

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